tags:

views:

346

answers:

4

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'

+1  A: 

Change this line:

cout<<"Enter choice : " << std::endl;
                    // ^^^^^^^^^^^^^^^

This print '\n' then forces the buffer to flush to the output so it can be read.

If you just want to flush the buffer:

cout<<"Enter choice : " << std::flush;

Drop the if statement. It's not needed you do the error checking with the switch.

// if( isalpha(choice) )
Martin York
+1  A: 

First, leave out the if( isalpha(choice) ). It is not needed, since any input but r, c or t will fall through to default.

Actually, it may be the cause of your problem. If the character entered is not alphanumeric, the function will end without displaying your error message.

In general my advice is: Don't think too complicated. The beauty of a switch is that you can act on exact matches, and all others will fall through to default

Treb
+1  A: 

What does "I tried this but it doesn't work" mean? What output are you getting? Do you mean it doesn't compile? (it won't in the state you're showing it). Do you mean that there's more to the program then what we see but it's producing unexpected results? There's not enough for us to debug this. For what it's worth, I didn't see anything wrong (aside from it obviously being incomplete):

#include <iostream>
using namespace std;

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<<"\nEnter choice : ";
    cin>>mainMenuChoice;
    return mainMenuChoice;
}

int main() {
    bool done = false;
    while(!done) {
        char choice = displayMainMenu();

        if( isalpha(choice) ) {

            switch(tolower(choice))
            {
            case 'r':
                    cout << "got 'r'\n";
                    break;
            case 'c':
                    cout << "got 'c'\n";
                    break;
            case 't':
                    cout << "got 't'\n";
                    done = true;
                    break;
            default:
                    cout<<"Invalid choice!\n"<<endl;
            }
        }
    }
    return 0;
}

And the output:

~ $ g++ input.cc -o input
~ $ ./input

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 : a
Invalid choice!


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 : c
got 'c'

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 : t
got 't'
~ $

Update I see it doesn't give the 'Invalid choice' message for non-numerics, but that can be fixed with removing the 'isalpha' check.

Ovid
+2  A: 

To get past the "cbbb" being accepted as 'c', you'll have to read in a line using getline() instead. Then you can check that only one character was entered, and then check which character that was.

If you use cin to read a char, it will only read the first character available in the input stream.

Eclipse