Quick help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int Menu()
{
    int choice;
    cout << "\nSurface Area Calculator" << endl <<
    endl << "S) Sphere" <<
    endl << "C) Cone" <<
    
    endl << "Q) Quit" << endl <<
    endl << "Enter your choice: ";
   
    do{
         cin >> choice;
         if(choice < 1 || choice > 5)
                   cout << endl << choice << " is an invalid choice. Try again: ";
    }while(choice < 1 || choice > 5);
    return choice;
}



i need the do to instead of check for numbers. make sure the choice is Q,S, or C ELSE invalid choice. but cant seem to figure it out.


my thought process is along the lines of
if choice = Q then return choice
if choice = S then return choice
if choice = C then return choice
else invalid
Last edited on
A nice trick is to search for the choice in the string "QSC". YOu can also avoid checking it twice:
1
2
3
4
5
6
7
char choice;
...
do {
    cin >> choice;
    if (strchr("QSC", choice)) return choice;
    cout << endl << choice << " is an invalid choice. Try again: ";
while (true);

Since you're searching in a fixed string, I think it's okay to use a C string here.
Topic archived. No new replies allowed.