简体   繁体   中英

Getting loop to redisplay message c++

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;
}

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;
}

I don't really think you need help on this one, so I was kind of reluctant to write this answer ^^.

A repetitive choice is typically implemented this way:

while (someCondition) {       // goes out if the condition isn't met
    cout << "The question" << endl;
    cin >> theAnswer;

    if (theAnswer == 1) {     // check the answer
        // do something
    }
                              // probably other checks
}

I suggest you practice a bit with if and while in small programs first, such as asking age, or counting the fibonnaci list

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM