Hello all,
I am just starting in C++ and I have the code below. When I run it and enter anything besides 1,2 or 3 (enter a 'd' for example), I am stuck in an infinite loop displaying the menu and "Invalid Choice, please try again." I tried cin.ignore() in DoMenu(), but it had no effect. I'm thinking my logic is messed up. Whatever the cause, I could *really* use a hint, please. :)
TIA,
UN
Code:
#include <iostream>
#include <limits>
using namespace std;
enum CHOICE {Encrypt = 1, Decrypt, Quit};
char DoMenu(void);
int main(void)
{
bool QUIT = false;
int choice = Encrypt;
while (!QUIT)
{
choice = DoMenu();
if (choice < Encrypt || choice > Quit)
{
cout << "\nInvalid Choice, please try again.\n\n";
continue;
}
switch( choice )
{
case Encrypt:
cout << "Encrypt Selected." << endl;
system("pause");
break;
case Decrypt:
cout << "Decrypt Selected." << endl;
system("pause");
break;
case Quit:
QUIT = true;
cout << "Exiting...\n\n";
break;
default:
cout << "Error in choice!\n";
QUIT = true;
break;
} //End Switch
} //End While
return 0;
} //End Main
char DoMenu(void)
{
int MenuChoice;
rewind( stdin );
system("cls");
cout << "\n*---------------*";
cout << "\n* 1. Encrypt\t*";
cout << "\n* 2. Decrypt\t*";
cout << "\n* 3. Quit\t*";
cout << "\n*---------------*\n\n";
cin >> MenuChoice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return MenuChoice;
}