Kelbin Gomez
Kelbin Gomez

Reputation: 27

C++ Do-while loop stopping

I got an assignment where we make a cmd prompt show up and display a flashcard game for multiplication. After inputting a correct answer a prompt shows up and asks the user to go "Again? Y/N." after the second input answer the prompt to ask the user doesn't show up and it's stuck on a "congratulations" message. This happens when I write in code to randomly generate two numbers for the game twice. one outside the while loop, and one inside while loop. If I leave one out the 2nd code for the random numbers it will run fine but will only display the same numbers over again. what I'm asking is how do I fix it so that it won't get stuck after the second answer input?

sample code below:

#include <iostream>

using namespace std;

int main()
{
    int num1, num2, ans, guess, count = 0;
    char choice;

    num1 = rand() % 12 + 1;  
    num2 = rand() % 12 + 1;
    //first number generator.
    ans = num1 * num2;

    do
    {
        {
            cout << num1 << " X " << num2 << " = ";
            cin >> guess;
            cout << "Wow~! Congratulations~! ";
            count++;

            num1 = rand() % 12 + 1;
            num2 = rand() % 12 + 1;
            //second number generator.

        } while (guess != ans);


        cout << "\nAgain? Y/N: ";
        cin >> choice;

    } while ((choice == 'y') || (choice == 'Y'));
    //after two turns the loop stops. Can't make a choice.

    cout << " Thanks for playing! Number of tries:" << count << endl;

    return 0;
}

Upvotes: 2

Views: 1115

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477617

The problem can be found here:

do
{
    {
        cout << num1 << " X " << num2 << " = ";
        cin >> guess;

As you can see, the second scope has no do statement. As a result it is only a codeblock.

You can solve it by writing a do statement for the second code block.

Because the do is not present in the second bracket ({), the while is interpreted as a while loop:

while (guess != ans);

or

while (guess != ans) {
}

this thus keeps looping until guess is not equal to ans. But since in the loop does not modify any of the two variables, the loop will keep iterating.


Other errors: note that the program is still incorrect, since it will claim you have answered the question, regardless of the answer. You can fix it by implementing this as follows:

int main()
{
    int num1, num2, ans, guess, count = 0;
    char choice;

    do {

       num1 = rand() % 12 + 1;
       num2 = rand() % 12 + 1;
       ans = num1 * num2;

       do {
            cout << num1 << " X " << num2 << " = ";
            cin >> guess;
            if(guess == ans) {
                cout << "Wow~! Congratulations~! ";
            } else {
                cout << "No, wrong!\n";
            }
            count++;

        } while (guess != ans);


        cout << "\nAgain? Y/N: ";
        cin >> choice;

    } while ((choice == 'y') || (choice == 'Y'));
    //after two turns the loop stops. Can't make a choice.

    cout << " Thanks for playing! Number of tries:" << count << endl;

    return 0;
}

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490623

I'd guess the problem is because your loops aren't quite what you think they are.

do
{

The code above has started a do loop.

    {

I suspect you intended to start another (nested) do loop here--but you left off the do, so it's just a block that gets entered, executed, and exited. Useless and pointless in this case.

        cout << num1 << " X " << num2 << " = ";
        cin >> guess;
        cout << "Wow~! Congratulations~! ";
        count++;

        num1 = rand() % 12 + 1;
        num2 = rand() % 12 + 1;
        //second number generator.

    } while (guess != ans);

You've formatted this as if the while were closing the nested do loop--but since you didn't actually create a nested do loop, this is just a while loop with an empty body. Its meaning would be more apparent with a little re-formatting:

    // second number generator
}

while (guess != ans)
    /* do nothing */
    ;

Upvotes: 4

Related Questions