mercredi 1 avril 2020

How would you combine a counter controlled loop with a flag controlled loop in c++?

I am making a guessing the number game in c ++ and am asked to combine a counter loop with a flag controlled loop. I am using a random number generator and the minimum amount of times the user can guess is 5 times, after the user has guessed 5 times the program is supposed to respond with the number the computer generated. The only problem is that I haven't been able to successfully combined the counter with the flag loop, and when compiled, my program only asks the use for the guess and after it iterates 5 times it ends the loop and does not respond with the number the computer had generated. I'm pretty sure this has to do with how I arranged the counter controlled loop, but I am having trouble setting it up so that the counter is incremented within the flag controlled loop and providing a valid statement for the flag controlled loop. I tried searching up for more examples of flag controlled loop and counter controlled loops as I am fairly new to these concepts, but have not been able to fix my program. Here is a part of my code:

while (!correct == (numGuesses < 5))
    {
        cout << "Guess the number the computer randomly picked between 1 - 100: ";
        cin >> guess;

        if (guess == number)
        {
            cout << "You guessed right, you win!" << endl << endl;
            correct = true;
        }
        else if (guess > number)
        {
        cout << "Sorry, your guess is too high" << endl;
        numGuesses += 1;
        }
        else if (guess < number)
        {
        cout << "Sorry, your guess is too low" << endl;
        numGuesses += 1;
        }
        else if (numGuesses == 5)
        {
            cout << "Sorry, you lost. The number is: " << number << endl << endl;
        }

    }

Aucun commentaire:

Enregistrer un commentaire