Do While Loop

I am trying to write a program that prompts the user to guess a number (has 20 attempts). The program is supposed to end after 20 incorrect guesses of if user guesses correctly. I am using a do-while loop and instead of prompting the user to guess a number after an incorrect guess it jumps to the end of the program and asks if the user wants to play again. What am I doing wrong? Thanks in advance.


// This program asks the user to guess a number between
// 1 and 100. The program will then generate a random
// number and output whether or not the user guessed
// correctly. The user has 20 attempts to guess correctly.
// The program will output the final number of wins and losses

#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>

using namespace std;

int main()
{
srand(time(0));
char playGame = 'Y';
int userNumber = 0;
int randomNumber = rand() % 100 + 1; // Generates random number between 1 and 100

// Initialize guess counts
int correctGuessCount = 0;
int tooLowGuessCount = 0;
int tooHighGuessCount = 0;
int count = 20;

cout << "This program gives you 20 attempts to guess a number between 1 and 100. ";
cout << "You will get a total of correct and incorrect guesses at the end of the program.";
cout << endl << endl;

do
{
cout << "Please enter a number between 1 and 100: ";
cin >> userNumber; // Prompt user for number
cout << "You entered: " << userNumber << endl;
count--;

if (userNumber < randomNumber)
{
cout << "Your guess was too low, try again. ";
tooLowGuessCount++;

}
else if (userNumber > randomNumber)
{
cout << "Your guess was to high, try again. ";
tooHighGuessCount++;
}
else if (userNumber = randomNumber)
cout << "Congratulations, you guessed correctly!";
correctGuessCount++;
}
cout << "Would you like to play again (enter Y for yes or N for No -- use capital letters only)?: ";
cin >> playGame;

while ((playGame == 'Y') && (count >= 0));

cout << endl;
cout << "Your total number of correct guesses is: " << correctGuessCount++ << endl;
cout << "Your total number of incorrect guesses is: " << tooLowGuessCount + tooHighGuessCount << endl;
cout << endl;
cout << "Thank you for playing the Random Numbers Game!" << endl << endl;

system("pause");
return 0;
}
Your "while" statement isn't actually joined to your do loop. You would need to put the two lines above your while statement inside the loop. So shift up
1
2
cout << "Would you like to play again (enter Y for yes or N for No -- use capital letters only)?: ";
cin >> playGame;
To be inside the curly brace above them.

EDIT: Your "else if (userNumber = randomNumber)" needs to be '==' not '='.
Last edited on
Thank you so much for your quick response. It works now.

I do have another problem now. When responding 'Yes' to play the game again the program uses the same random number. I tried to include the random number generator a second time at the beginning of the Do-While loop so that the program generates a new number the program never generates a number.

In your else if statement that checks if the users input is correct. If his answer == randomNumber - Generate a new random number in there if you wish.
Thank you Mythios. The program works just fine now.
Glad I could help.
Topic archived. No new replies allowed.