0

I am writing code integrating Python and C++, the menu must loop after an option is selected and results are displayed unless the menu option to exit is chosen. The python integration code and python code are left out for conciseness. I am able to validate if an integer outside of the menu options is used, however entering anything other than an integer value causes the printMenu(); system("pause"); to loop infinitely. I will also need to provide the same validation for userNumber. Any help is appreciated. Thank you.

void printMenu() {
    cout << "1: Display a Multiplication Table" << endl;
    cout << "2: Double a Value" << endl;
    cout << "3: Exit" << endl;
    cout << "Enter your selection as a number 1, 2, or 3." << endl;
}
void main() {
    int userOption;
    int userNumber;
    bool loopMenu;

    loopMenu = true;

    while (loopMenu) {
        printMenu();
        system("pause");
        cout << endl;
        cout << "Enter selection: ";
        cin >> userOption;
        cout << endl;

        if (userOption != 1 && userOption != 2 && userOption != 3) {
            cout << "Please enter a number between 1 and 3 only." << endl;
        }

        if (userOption == 1) {
            cout << "Enter a value: ";
            cin >> userNumber;
            cout << endl;

            callMultTable("MultTable", userNumber);
            cout << endl << endl;
        }

        if (userOption == 2) {
            cout << "Enter a value: ";
            cin >> userNumber;
            cout << endl;
            cout << callIntFunc("DoubleValue", userNumber) << endl << endl;
        }
                
        if (userOption == 3) {
            cout << "Program exiting. Goodbye." << endl;
            exit(0);
        }
    }
kmshaw
  • 1
  • 1
  • Does this answer your question? [Infinite loop with cin when typing string while a number is expected](https://stackoverflow.com/questions/5864540/infinite-loop-with-cin-when-typing-string-while-a-number-is-expected) – drescherjm Jun 08 '22 at 20:31

1 Answers1

0

A workaround was to change input to a string, add a variable for char, set char = string.at(0) - 48 (to get number from ascii value).

kmshaw
  • 1
  • 1