Harry
Harry

Reputation:

error-handling-when-taking-user-input part2

I made previously a question: 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??

Upvotes: 1

Views: 221

Answers (2)

Eclipse
Eclipse

Reputation: 45533

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.

Upvotes: 1

Bill the Lizard
Bill the Lizard

Reputation: 405985

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.

Upvotes: 2

Related Questions