eng
competition

Text Practice Mode

Print C++ code

created May 22nd, 03:22 by emadalqassim


0


Rating

279 words
1 completed
00:00
#include <iostream>
#include <string>
 
using namespace std;
 
class Menu {
public:
    void displayMenu() {
        cout << "=============================\n";
        cout << "          Menu Options       \n";
        cout << "=============================\n";
        cout << "1. Enter text to display\n";
        cout << "2. Enter two numbers to sum\n";
        cout << "3. Enter a string to get its length\n";
        cout << "4. Exit\n";
        cout << "=============================\n";
        cout << "Choose an option: ";
    }
};
 
class TextHandler {
public:
    void displayText() {
        cin.ignore(); // Ignore remaining newline character from previous input
        string text;
        cout << "Enter the text: ";
        getline(cin, text);
        cout << "The text you entered is: " << text << "\n";
    }
};
 
class NumberHandler {
public:
    void sumNumbers() {
        int num1, num2;
        cout << "Enter the first number: ";
        cin >> num1;
        cout << "Enter the second number: ";
        cin >> num2;
        cout << "The sum of the numbers is: " << num1 + num2 << "\n";
    }
};
 
class StringHandler {
public:
    void displayStringLength() {
        cin.ignore(); // Ignore remaining newline character from previous input
        string str;
        cout << "Enter the string: ";
        getline(cin, str);
        cout << "The length of the string is: " << str.length() << "\n";
    }
};
 
int main() {
    Menu menu;
    TextHandler textHandler;
    NumberHandler numberHandler;
    StringHandler stringHandler;
 
    int choice;
    bool running = true;
 
    while (running) {
        menu.displayMenu();
        cin >> choice;
 
        switch (choice) {
            case 1: {
                textHandler.displayText();
                break;
            }
            case 2: {
                numberHandler.sumNumbers();
                break;
            }
            case 3: {
                stringHandler.displayStringLength();
                break;
            }
            case 4: {
                cout << "Exiting the program...\n";
                running = false;
                break;
            }
            default: {
                cout << "Invalid option, please try again.\n";
                break;
            }
        }
 
        cout << "\n"; // Add a new line between operations
    }
 
    return 0;
}
 

saving score / loading statistics ...