Hello everybody,
I am a beginner in c++ and I have a small problem:
my code displays a simple menu to the user providing three options:
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";
What I want is now is that, when a user enters:
- aer ->invalid input entered, try again
- 1 or any other number->invalid input entered, try again
- rt ->invalid input entered,try again (here the first character is correct but he entered 2 characters)
- cf ->invalid input entered, try again
ONLY IF THE USER ENTERS CORRECTLY ONE OF THE 3 SIMPLE CHARACTERS (r,c,t NONSENSITIVE CASE) to do sth. Otherwise a massage for invalid input should be printed and then the main menu should appear again
I tried this but it doesnt work:
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();
if( isalpha(choice) )
{
switch(tolower(choice))
{
case 'r':
DoSTH1();
break;
case 'c':
DoSTH2();
break;
case 't':
DoSTH3();
break;
default:
cout<<"Invalid choice!\n"<<endl;
}
}
}
return 0;
}
I hope u can help me
ADDED: When i enter by mistake for example: cbbbbbb it takes it as if it was 'c'