0

I have some issues with looping. I am trying to loop multiple statements. Code is below.

int MenuSelect () {
    cout << endl;
    cout << YELLOW  << "Enter 1 for info" << endl;
    cout << " " << endl;
    cout << "Enter 2 to Start" << endl;
    cout << " " << endl;
    cout << "Enter 3 to view settings" << endl;
    cout << " " << endl;
    cout << "Enter 4 to quit" << endl;

     int selected = 0;
    string input;
    cin >> input;
    if (stringstream(input) >> selected) {
        return selected;
    }
    else {
        return -1;
    }

    return 0;

};    

int Menu(int) {

    int selected {};
    while ((selected = MenuSelect()) == 1) {

        printmessage();


    }
    if (selected == 3) {

        cout << "Current Settings" << endl;

        somefunction();
    }

    else if (selected == 2) {

        cout << "Starting Game..... " << endl;
    }

    else if (selected == 4) {

        cout << "Exiting....." << endl;
        exit (3);
    }

    else {

        cout << "Invalid Entry" << endl;
        exit (3);
    }

    cout << "Below is the Deck of cards and you will get to choose 5 cards to play with. Choose wisely." << endl;

    return 0;

};

So as you can see the user can see the menu, then if and else statements do the work. At the moment I have managed to loop the first menu select so if the user enters 1 it will print the message and then loop back to the menu. What I want is to also loop the 3 - current settings and if the user enters a invalid number. I've tried to accomplish it but I can't seem to do it.

2
  • Move those if/else if blocks into the loop body, and then either change your loop condition to while ((selected = MenuSelect()) > 0) (works in this case since your 4 case just calls exit), or pull the selected = MenuSelect() out of the loop and run it again at the end of the loop body. Commented Apr 15, 2020 at 14:34
  • @0x5453 thank you that fixed the issue Commented Apr 15, 2020 at 14:38

1 Answer 1

1

You probably want to repeatedly read user input and react accordingly? You can put all the ifs into the while:

int Menu(int) {

    int selected {};
    bool loop = true;

    while (loop) {
        selected = MenuSelect();

        if(selected == 1) {
            printmessage();
        }
        else if (selected == 3) {
            cout << "Current Settings" << endl;

            somefunction();
        }
        else if (selected == 2) {
            cout << "Starting Game..... " << endl;
        }
        else if (selected == 4) {
            cout << "Exiting....." << endl;
            loop = false;
        }
        else {
            cout << "Invalid Entry" << endl;
            loop = false;
        }
    }

    cout << "Below is the Deck of cards and you will get to choose 5 cards to play with. Choose wisely." << endl;

    return 0;

};

Using a variable loop to exit the while is just personal preference, you could as well have an endless loop

while(true)

and use break; to exit the loop or exit() to exit the whole program, as you do now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.