2

OKay, so I'm new to the whole do while loop thing, and I'm trying to make a main menu, here's my code below:

int main()
{
    int choice;
    char sure;
    bool quit = false;
    char ctrl;

    do
    {
        cout << "Main Menu." << endl
             << "1. New Game." << endl
             << "2. Load Game." << endl
             << "3. Exit." << endl
             << "Your choice: ";
        cin >> choice;

        if (choice == 1)
        {
            cout << "Are you sure you wish to start a new game? (Y/N) ";
            cin >> sure;

            if (sure != 'N' || sure != 'n')
            {
                ctrl = 'a';
                quit = true;
            }
        }
        else if ( choice == 2)
        {
            ctrl = 'b';
            quit = true;
        }
        else
            quit = true;

        }
    }
    while (quit == true);

    if (ctrl = 'a')
         cout << "New Game." << endl;
    else if (ctrl = 'b')
         cout << "Load Game." << endl;
    else
         cout << "Goodbye." << endl;

    return 0;
}

there are a few getchar() thrown in there. But the only problem is as you'll probably figure out is that after I do everything it just restarts again, and not exit the loop. What is the problem in the code?

Thanks

2
  • 2
    ¤ The main problem is that you didn't fix what I commented on last time you posted. When you don't fix it, it's going to continue to be wrong. Just to give you the opportunity to get on track, replace while(quit == true) with while(!quit), and start thinking about what that means for the rest of the code. Cheers & hth., Commented Dec 4, 2011 at 22:44
  • Don't just replace the wrong code with the correct one. The answers and the question itself won't make any sense anymore. Commented Dec 4, 2011 at 23:42

3 Answers 3

5

I think you meant while (quit != true);

And remember, comparison is done with ==, if (ctrl = 'a') assigns 'a' to ctrl..

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

1 Comment

I don't normally upvote the not-first correct answer, but the extra information in this totally deserves it. It effectively preempts the OP's next question in 10 minutes!
4

You are not setting quit to false anywhere and your loop runs while quit equals true. You need to get the meaning of your boolean values straight or simply change the while part to while(!quit). I'd rather to the first.

2 Comments

I have ... bool quit = false; :L
@user1079559 But you set it to true every time you actually want to quit. Read everything out loud: quit is false; now do statements and set quit to true under some conditions; repeat this while quit equals true. The flaw becomes obvious as soon one of the conditions to set quit is met.
2

Shouldn't you just change

    while (quit == true);

to

    while (quit != true);

?

Maybe you know it, but I'll just repeat how

do{
//...
} while(condition)

loop works. You iterate until the condition is false. In your case it was always true, that's why you had an infinite loop.

P.S. Take a loot at this answer also. Yet another error was described there.

Imroved code with hopefully all corrected mistakes is here.

3 Comments

I've done that and I guess I've gotten out of the loop but the command prompt is not just exiting despite putting getchar() on the if (ctrl == ... ) part at the code. Any idea's?
@user1079559 be sure to correct if(ctrl = 'a') and if(ctrl = 'b'). Also delete } just before while! It works fine for me! Now you are on your own :)
done all of that and it's still the same problem XD it's probably so stupidly simple, I'll post the code I have now.

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.