I made previously a question: http://stackoverflow.com/questions/236354/error-handling-when-taking-user-input
and I made the suggested changes:
char displayMainMenu()
{
char mainMenuChoice;
cout << "\nQuadratic equation: a*X^2 + b*X + c = 0 main menu: ";
cout << "\n <r> Give new coefficients";
cout << "\n <c> Calculate equations solutions";
cout << "\n <t> Terminate the program";
cout<<"Enter choice : ";
cin>>mainMenuChoice;
return mainMenuChoice;
}
int main()
{
bool done = false;
while(!done)
{
char choice = displayMainMenu();
switch(tolower(choice))
{
case 'r':
cout<<"Entered case 'r'";
break;
case 'c':
cout<<"Entered case 'c'";
break;
case 't':
cout<<"Entered case 't'";
break;
default:
cout<<"Invalid choice! Try again"<<endl;
}
}
return 0;
}
The new problem is that if the user enters by mistake lets say "ter" i get the following :( :
Quadratic equation: a*X^2 + b*X + c = 0 main menu:
<r> Give new coefficients
<c> Calculate equations solutions
<t> Terminate the program
Enter choice : ter
Entered case 't'
Quadratic equation: a*X^2 + b*X + c = 0 main menu:
<r> Give new coefficients
<c> Calculate equations solutions
<t> Terminate the program
Enter choice : Invalid choice! Try again
Quadratic equation: a*X^2 + b*X + c = 0 main menu:
<r> Give new coefficients
<c> Calculate equations solutions
<t> Terminate the program
Enter choice : Invalid choice! Try again
How could I avoid this from happening??