0

I am trying to get a program to display a menu with options, then given the user input, display a certain message. After it displays their message I want the loop to go back to displaying the message until they choose the quit option. How do I get the loop to return to displaying the menu after any choice of 1-3?

#include <iostream>
#include <iomanip>
using namespace std;


int main(){
int menu_choice;
cout << "Select a numerical option:" << endl << "=== menu ===" << endl << "1. Fox" << endl << "2. Bunny" << endl << "3. Sloth" << endl << "4. Quit" << endl;
cin >> menu_choice;
while (menu_choice >= 1 && menu_choice <= 4)
{
    while (menu_choice == 1)
    {
        cout << "1 work" << endl;
        menu_choice = 0;
        continue;
    }
    while (menu_choice == 2)
    {
        cout << "2 work" << endl;
        menu_choice = 0;
        continue;
    }
    while (menu_choice == 3)
    {
        cout << "3 work" << endl;
        menu_choice = 0;
        continue;
    }
    while (menu_choice == 4)
    {
        cout << "4 work" << endl;
        menu_choice = 0;
        continue;
    }
}
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    This is just crazy imho `while (menu_choice == 1) { cout << "1 work" << endl; menu_choice = 0; continue; }` - why would you use a `while` loop here? You set the loop condition to false unconditionally. And what's that `continue` for? This is just bonkers. – Jesper Juhl Sep 23 '20 at 21:22
  • That's a lot of whiles :O most of them are useless. Did you learn about `if` or `switch` yet? It might help you for your menu_choice checks. – Vélimir Sep 23 '20 at 21:24
  • Looks like you wanted to use `if`s or a `switch` rather than those inner `while` loops. Rewrite with a `switch` and add a `default` case to handle what happens if none of the options are taken and then see if you still need to ask this question. – user4581301 Sep 23 '20 at 21:25

2 Answers2

1

Your code is displaying the menu only 1 time. If the user enters an invalid choice, you exit right away. If the user enters a valid choice, you do the chosen work, but then you exit rather than display the menu again. All of your while..continue loops are completely useless, they only run 1 time at most, setting the menu_choice to 0, which breaks your outer while loop so it runs only 1 time as well.

You need an outer loop that runs continuously, displaying the menu each time, until you are actually ready to exit.

You should also replace the useless while..continue loops with if/else blocks, or better a single switch block.

Try something more like this instead:

#include <iostream>
#include <limits>
using namespace std;

int main()
{
    int menu_choice;

    do
    {
        cout << "Select a numerical option:" << endl
             << "=== menu ===" << endl
             << "1. Fox" << endl
             << "2. Bunny" << endl
             << "3. Sloth" << endl
             << "4. Quit" << endl;

        if (!(cin >> menu_choice))
        {
            menu_choice = 0;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        switch (menu_choice)
        {
            case 1:
                cout << "1 work" << endl;
                break;

            case 2:
                cout << "2 work" << endl;
                break;

            case 3:
                cout << "3 work" << endl;
                break;

            case 4:
                cout << "4 work" << endl;
                break;

            default:
                cout << "Bad choice! Try again" << endl;
                break;
        }
    }
    while (menu_choice != 4);

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

@Remy Lebeau was quicker posting and his answer is well written, but since I was done writing the code and you might benefit looking at different implementations I'm posting the code. I deliberately chose not to use switch since you might not have seen it before.

#include <iomanip>
#include <iostream>

using namespace std;

int main() {
    bool quit = false;
    int menu_choice;

    while(!quit) {
        cout << "Select a numerical option:" << endl
             << "=== menu ===" << endl
             << "1. Fox" << endl
             << "2. Bunny" << endl
             << "3. Sloth" << endl
             << "4. Quit" << endl;
        if(cin >> menu_choice) {
            if(menu_choice == 1) {
                cout << "1 work" << endl;
            }
            if(menu_choice == 2) {
                cout << "2 work" << endl;
            }
            if(menu_choice == 3) {
                cout << "3 work" << endl;
            }
            if(menu_choice == 4) {
                cout << "Bye" << endl;
                quit = true;  // Set quit to true to stop the while loop
            } else {
                cout << "Invalid number" << endl;
            }

        } else {
            cout << "Bad input" << endl;
            cin.clear();
            cin.ignore();
        }
    }
    return 0;
}

Note that the cin.ignore and cin.clear are important and often confuse people new to this. Read this question for the details.

darcamo
  • 3,294
  • 1
  • 16
  • 27