| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #include <SFML/Graphics.hpp> #include <SFML/Window.hpp> #include <vector> #include <string> #include <iostream> using namespace std; using namespace sf; // Item structure struct Item {     string name;     RectangleShape shape; }; // Function to draw items void drawItems(RenderWindow& window, const vector<Item>& items) {     window.clear(Color::White);     for (const auto& item : items) {         window.draw(item.shape);     }     window.display(); } int main() {     RenderWindow window(VideoMode(800, 600), "Simple CRUD Application");     vector<Item> items;     Font font;     if (!font.loadFromFile("arial.ttf")) {         cout << "Error loading font" << endl;         return -1;     }     Text text;     text.setFont(font);     text.setCharacterSize(24);     text.setFillColor(Color::Black);     bool running = true;     while (running) {         Event event;         while (window.pollEvent(event)) {             if (event.type == Event::Closed) {                 running = false;             }         }         if (Keyboard::isKeyPressed(Keyboard::A)) {             // Add item             Item newItem;             newItem.name = "Item " + to_string(items.size() + 1);             newItem.shape.setSize(Vector2f(100, 50));             newItem.shape.setFillColor(Color::Green);             newItem.shape.setPosition(Vector2f(10, 10 + 60 * items.size()));             items.push_back(newItem);             drawItems(window, items);         }         if (Keyboard::isKeyPressed(Keyboard::R) && !items.empty()) {             // Remove item             items.pop_back();             drawItems(window, items);         }         if (Keyboard::isKeyPressed(Keyboard::U) && !items.empty()) {             // Update item             items.back().shape.setFillColor(Color::Red);             drawItems(window, items);         }         if (Keyboard::isKeyPressed(Keyboard::D) && !items.empty()) {             // Display items             string itemText;             for (const auto& item : items) {                 itemText += item.name + "\n";             }             text.setString(itemText);             window.clear(Color::White);             window.draw(text);             window.display();         }     }     return 0; } | 
Explanation
- Dependencies:
- This program uses the SFMLlibrary for creating the graphical user interface (GUI). Ensure you haveSFMLinstalled and properly linked in your project.
 
- This program uses the 
- Item Structure:
- Itemstruct holds the name and graphical representation (shape) of an item.
 
- Draw Items Function:
- drawItems(RenderWindow& window, const vector<Item>& items): Clears the window and draws all items from the vector.
 
- Main Function:
- Initialization: Sets up the SFMLwindow and font.
- Event Handling: Checks for user inputs to perform CRUD operations.
- Add Item (Akey): Creates a new item with a green rectangle shape and adds it to the list.
- Remove Item (Rkey): Removes the most recently added item.
- Update Item (Ukey): Changes the color of the most recently added item to red.
- Display Items (Dkey): Shows the names of all items in the window.
 
- Add Item (
 
- Initialization: Sets up the 
- Running the Application:
- The application continues to run until the window is closed. During runtime, pressing the keys will add, remove, update, or display items as specified.
 
Note:
- The program uses SFMLfor GUI, so ensure thatSFMLis installed and linked correctly.
- Font file arial.ttfmust be present in the working directory or specify the path to a valid font file.
