#includeint main() { sf::RenderWindow window(sf::VideoMode(200, 200), "Button example"); // Create a button object sf::RectangleShape button(sf::Vector2f(100,50)); button.setPosition(50,75); // Set button label text sf::Font font; font.loadFromFile("arial.ttf"); sf::Text label("Click me", font, 16); label.setPosition(button.getPosition().x+15, button.getPosition().y+15); // Event loop to handle button clicks while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { if (button.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y)) { // Change button text dynamically label.setString("Clicked!"); } } } // Draw button and label to window window.clear(sf::Color::White); window.draw(button); window.draw(label); window.display(); } return 0; }
#includeIn this example, we have used the standard C++ library to get user input and display custom output with dynamic text content. The setString() method is used implicitly in the second output statement, where the value of the input variable is changed dynamically. Package library: The SFML graphics library is used in Example 1, while the standard C++ library is used in Example 2.#include int main() { std::string input = ""; // Get user input std::cout << "Enter your name: "; std::getline(std::cin, input); // Display output with custom text std::cout << "Hello, "; std::cout << input; std::cout << ". Welcome to our program!" << std::endl; // Change output dynamically input = "New User"; std::cout << "Hello, "; std::cout << input; std::cout << ". Welcome to our program!" << std::endl; return 0; }