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);
}
}