C++ range checking

ok well I'm using a switch statement for variable "choice"...

I want to either:

1) check that the value choice is >=0 and <=5... if not display "Invalid input" and if correct, return(choice)

OR

2) change variable choice to string, and do a stringcompare, to check if choice = 0, if it is close the program (return(0)), if not follow that by a switch statement

I personally think #1 will be easier...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int displayMenu(int &choice, string id, string name)
{
clrscr();
  
    cout << "Book Price (Id: " << id << " Name: " << name << ") ver 1.0";
    cout << "\n\nMAIN MENU\n";
    cout<< setfill('_')<<setw(53)<<"_"<<endl;
    cout << "\n| 0. Exit      |  1. Enter book   |  2. Find book   |";
    cout << "\n| 3. List all  |  4. Delete book  |  5. Update book |\n";
    cout<< setfill('_')<<setw(53)<< "_" <<endl;
    cout << "\n\nYour choice --> ";
    cin >> choice;
    
    return (choice);
}       
int main()
{
 int choice;
  
  while(choice != 0){ //<--- error here
  displayMenu(choice,id,name);
   
switch(choice)
{
 case 1:

  //blah blah blah
         
Last edited on
change variable choice to string, and do a stringcompare, to check if choice = 0
What would you gain from that?
because if i enter a character rather than a number, the program crashes...
If you need more code i can upload it...
while (!(std::cin >>choice)); //keeps going until the user enters something that can fits the type
1
2
3
4
5
6
7
8
9
10
switch (choice)
{
  case 0: ...
  case 1: ...
  case 2: ...
  case 3: ...
  case 4: ...
  case 5: ...
  default: cout << "Hey foo! Choose properly!\n";
}

There is, however, a design error in your code. First is the name of your function: "displayMenu". The function does more than simply display the menu -- it also gets user input. You might want to name your function more appropriately.

The second is using the int reference as an argument. Why are you doing that? Just return the proper value from the function.

A proper exit condition of the function might very well be to only return a valid choice.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int menu(string id, string name)
{
    int choice;

    // This part displays the menu
    clrscr();
    cout << "Book Price (ID: " << id << "  Name: " << name << ") ver 1.0\n\n";
    cout << "MAIN MENU\n";
    ...
    cout << setfill('_') << setw(53) << "_" << "\n\n";

    // This part gets a valid user input
    while (true)
    {
        cout << "Your choice --> ";
        cin >> choice;

        // if choice was valid, we're done
        if (cin && (choice >= 0) && (choice <= 5))
            break;

        // if choice was invalid (or something worse), we need to try again
        if (!cin)
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        cout << "You must choose in 0 through 5.\n";
    }

    return choice;
}

After this, your calling code only needs to know that a valid choice is always returned.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
    bool done = false;
    while (!done)
    {
        switch (menu("12345", "Gulliver's Travels"))
        {
            case 0: done = true;      break;
            case 1: add_book();       break;
            case 2: find_book(...);   break;
            case 3: list_all_books(); break;
            case 4: delete_book();    break;
            case 5: update_book(...);
        }
    }
    return 0;
}

I really don't know how your program is structured, so the main() program example I gave there will obviously need some adjustment...

Hope this helps.
@helios You forgot to clear the input...
operator>>() doesn't unconditionally pop the buffer? Damn.
Last edited on
Topic archived. No new replies allowed.