tags:

views:

82

answers:

2

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??

+1  A: 

In your displayMainMenu() function, instead of reading in a char, read in a string. Throw out (with a warning) any input that is greater than one character in length.

You can use

char str[101]
std::cin.getline(str, 101);

in place of

cin >> mainMenuChoice;

in order to read the string.

Bill the Lizard
I did:char displayMainMenu(){ char mainMenuChoice[101]; cout<<".."; cout<<".."; cout<<"Enter choice : "; std::cin.getline(mainMenuChoice, 101); return mainMenuChoice;} but gives me:'return' : cannot convert from 'char [101]' to 'char'
return mainMenuChoice[0];
Bill the Lizard
...and do the error handling inside the mainMenuChoice function.
Bill the Lizard
+1  A: 

Try this:

string displayMainMenu()
{
    string 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 << "\nEnter choice : ";
    getline(cin, mainMenuChoice);
    return mainMenuChoice;
}

int main()
{
    bool done = false;
    while(!done)
    {
     string choice = displayMainMenu();
     if (choice.size() > 1 || choice.size() < 0)
      cout<<"Invalid choice! Try again"<<endl;

     switch(tolower(choice[0]))
     {
     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;
}

Use getline(istream, string &) to read in a full line at a time (not including the eol). Check that it's the right length, and then look at only the first character.

Eclipse