glashunti
glashunti

Reputation: 355

Returning to the loop c++

I'm trying to do a simple game Guess Number and created a while condition to do that, but I want to insert a question to play again if the player type "Y" on the queue or close the window if the player type "N". My way is not working and I didn't find a solution for this problem.

int main()
{

    int guess, number;
    char again;

    srand(time(0));
    number = rand() % 1000 + 1;

    while (guess != number)
    {
        std::cout << "Enter the number guess between 1 and 1000: ";
        std::cin >> guess;

        if (guess < number)
        {

            std::cout << "Is more than this" << std::endl;
        }
        else if (guess > number)
        {
            std::cout << "Is less than this" << std::endl;

        }
        else if (guess < 1)
        {

            std::cout << "The value to guess is between 1 and 1000" << std::endl;
        }
        else if (guess > 1000)
        {
            std::cout << "The value to guess is between 1 and 1000" << std::endl;

        }
        else
        {

            std::cout << "This is the number" << std::endl;
            std::cout << "Do want play again? [Y/N ]" << std::endl;
            std::cin >> again;
            if (again == 'N' || again == 'n')
            {
                break;
            }
            else if (again == 'Y' || again == 'y')
            {
                continue;
            }
        }

    }

Upvotes: 0

Views: 88

Answers (2)

Aconcagua
Aconcagua

Reputation: 25516

Starting with some presumably yet unrecognised problem: You won't reach all of your if branches:

if (guess < number)
{ }
else if (guess > number)
{ }
// now if you really get to the following else, guess was neither
// smaller nor greater than number, i. e. is EQUAL!
else if (guess < 1)
{ /* won't ever be entered as number(!) is never < 1 (be aware: guess == number) */ }
else if (guess > 1000)
{ /* won't ever be entered as number is never > 1000 */ }
else
{ }

You can solve in two variants, by moving the unreachable checks either in front of the initial ones:

if (guess < 1)
{ }
else if (guess > 1000)
{ }
else if (guess < number)
{ }
else if (guess > number)
{ }
else
{ }

or into them:

if (guess < number)
{
    if (guess < 1)
    { }
    else
    { }
}
else if (guess > number)
{
    if (guess > 1000)
    { }
    else
    { }
}
else
{ }

Now to the actual problem, let's consider the else:

 // be aware that guess == number now!
 if (again == 'N' || again == 'n')
 {
     break; // fine so far...
 }
 else if (again == 'Y' || again == 'y')
 {
     continue;
     // re-enters the loop - be aware that the condition is still checked!!!
 }
 // and if none of all was entered???
 // as is, we'd just go on with the loop body - as this was the last statement,
 // though, the loop will be re-entered by checking the condition; i. e. if
 // getting here, we do effectively exactly the same as in the second if check
 // above...

OK, so you (implicitly) defined a default of 'y'. You then could just simply remove the second if (else if == y) and nothing would change.

However, the loop condition is not true any more (guess == number still applies!). Easiest now: just make an endless loop of:

for(;;)
{
    if(again == 'n')
        break;
    // obsolete, just drop it:        
    //if(again == 'y')
    //    continue;
}

I personally, though, would rather have the 'n' as default (so typing 'x', 'q', 'a' all result in exiting as well), so I'd rather have:

for(;;)
{
    if(again != 'y' && again != 'Y')
        break;
}

Upvotes: 0

a_pradhan
a_pradhan

Reputation: 3295

When you correctly guess the number, your while condition becomes false and hence the loop exits (i.e. guess becomes equal to number, and hence the condition guess != number is false). Try changing to condition of the while loop.

char again = 'Y';
while (again == 'Y' || again == 'y') { ... }

Upvotes: 4

Related Questions