Following is a part of my VC++ code being coded in VS2010.
do
{
std:: cout << "\nOPTIONS:\n";
std:: cout << "\n1. Get all session data.\n";
std:: cout << "\n2. Get Nth session data\n";
std:: cout << "\n3. Get Next session data\n";
std:: cout << "\n4. Get Previous session data\n";
std:: cout << "\n5. Get total no of sessions\n";
std:: cout << "\n6. Exit\n";
std:: cout << "\nEnter Your Option: ";
std :: cin >> option;
switch(int(option))
{
case 1:
{
data.ReadSetupFile();
break;
}
case 2:
{
break;
}
.....
......
case 6:
{
std::cout<<"\nARE YOU SURE YOU WANT TO EXIT?(Press y for yes and any other character for no)\n";
cin >> opt;
break;
}
default:
{
std::cout << "\nINAVALID OPTION!!TRY AGAIN\n";
break;
}
}
std::cout<<"\nDO YOU WANT TO CONTINUE?(Press y for yes and any other character for no)\n";
cin >> opt;
}while(opt=='y');
However, if I insert a character instead of an integer as value of option
, the menu option is going on printed in the output console without termination until I use ctrl+c
. On using this break strategy I get the message as :
First-chance exception at 0x75936da7 in SetupAPIReader.exe: 0x40010005: Control-C.
Why is the execution not terminating?
If the given input is not of expected type, the failbit
of cin
is set. Checking for this bit and clearing will reset the value to initial value of the variable.
if(!cin)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}