int
menu()
{
int choice;
cout << " **** MENU **** " << endl;
cout << "[1] Option 1" << endl;
cout << "[2] Option 2." << endl;
cout << "[3] Exit the program.\n\n" << endl;
cout << "NOTE: *** Use CTRL + C to exit the program in case of error. ***\n\n" << endl;
cout << "Choose an option:" << endl;
if (!(cin >> choice)) {
cout << "ERROR";
cin.clear();
cin.ignore();
} else {
cin >> choice;
cin.ignore();
}
cout << "\n\n";
return (choice);
}
There is no loop in this code, so I guess you are experiencing a freeze instead? If you debug and run the code line by line, where does the program get stuck?
Also the else{} block @ line 17 is not necessary.
I would write:
1 2 3 4 5 6 7 8
while (true)
if (!cin >> choice)
{
cout << " ERROR: Please try again." << endl;
cin.clear();
cin.ignore();
}
elsebreak;