I am using netbean ide for c++ study I would like to force the user to pick only number between 1 to 3
int displayMenu()
{
while(true)
{
cout<<"Please choose one of following options"<<endl;
cout<<"1. deposit"<<endl;
cout<<"2. withdraw"<<endl;
cout<<"3. exit"<<endl;
int input;
cin >>input;
if(input>=1 && input<=3 && cin)
{
return input;
break;
}
else
{
cout<<"You have only 3 options. Please chooses 1,2 or 3"<<endl;
}
}
}
It works fine if the input is int number If input is less then 1 or greater than 3, this function re-ask user to input number btw 1 and 3.
However, if the input is character such as 'f', it does an infinite loop. This function know that 'f' is wrong input..
I did my own research in the Internet. !cin and cin.fail() do not work.
Can you help me?