Game replays and wont quit!

Hi! For a class that I am taking we are supposed to make a guessing game that creates a random number between 1 and 1000 and we have to guess it.
I have used all the parts that we have learned in the class already.
But for some reason the game replays even if I type "n" which is supposed to be No and supposed to continue to say "Press any key to exit..." then exit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 #include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;
const int MAX_VALUE = 1000;
int main()
{
	srand(time(0));
	int random = rand() % 1000;
	random = rand() % 1000 + 1;
	int guess;
	bool again = true;
	char replay;
	do
	{
		cout << "Guess a number: ";
		cin >> guess;
		int counter = 0;
		while (true)
		{
			if (guess > random)
			{
				counter = counter + 1;
				cout << "Too high!. Try again: ";
				cin >> guess;
			}
			else if (guess < random)
			{
				counter = counter + 1;
				cout << "Too low!. Try again: ";
				cin >> guess;
			}
			else if (guess == random)
			{
				cout << "You guessed it!" << endl;
				cout << "Would you like to play again? Y/N: ";
				cin >> replay;
				cout << "\n\n";
				if (replay != 'y')
				{
					again = false;
					cout << "Thanks for playing!" <<endl;
					break;
				}
			}
		}
	} while (guess);
	system("pause");
	return 0;
}


Please help!
Last edited on
Try changing line 48 to:
} while (again);
Thanks that worked! idk why I wrote it as guess, probably read the wrong line at the top or something!

New problem however... With the new code the game doesnt replay when "y" is entered
Last edited on
Try changing line 40 to:
if (replay == 'y' || replay == 'Y')

Edit: See my post below.
Last edited on
Run it in a debugger or add temporary code to print the value of replay. I think you'll find that replay is the newline character after the final guess rather than the letter that they enter. Try making replay a string instead and check the first character.
What dhayden is hinting at:
After doing integer input with cin >> guess there is an extra invisible character leftover in the input stream that cin uses behind the scenes. Simply add cin.ignore(); after every cin >> guess; to remove leftovers. This really only matters when you swap back and forth between integer inputs and char inputs.
Here is the new code... It still doesnt seem to want to work :(
When "y" or "Y" is entered for "Would you like to play again?" It still says "Thanks for playing!" When Its supposed to restart the game

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main()
{
	srand(time(0));
	int random = rand() % 1000;
	random = rand() % 1000 + 1;
	int guess;
	bool again = true;
	char replay;
	do
	{
		cout << "Guess a number: ";
		cin >> guess;
		int counter = 0;
		while (true)
		{
			if (guess > random)
			{
				counter = counter + 1;
				cout << "Too high!. Try again: ";
				cin >> guess;
				cin.ignore();
			}
			else if (guess < random)
			{
				counter = counter + 1;
				cout << "Too low!. Try again: ";
				cin >> guess;
				cin.ignore();
			}
			else if (guess == random)
			{
				cout << "You guessed it!" << endl;
				cout << "Would you like to play again? Y/N: ";
				cin >> replay;
				cout << "\n\n";
				if (replay == 'y' || replay == 'Y')
				{
					again = false;
					cout << "Thanks for playing!" <<endl;
					break;
				}
			}
		}
	} while (again);
	system("pause");
	return 0;
}
Look at what your code is doing:

1
2
3
4
5
6
7
8
9
cout << "Would you like to play again? Y/N: ";  // ask them if they want to play again
cin >> replay; // get their input
cout << "\n\n";
if (replay == 'y' || replay == 'Y')  // if they input "Y", indicating they DO want to play again
{
    again = false;  // <- you set again to FALSE, signaling the program to close
    cout << "Thanks for playing!" <<endl;
    break;
}
Now that I look more closely it looks like the OP wanted to check for "N" in the first place. (Since it was originally replay != 'y')
So perhaps changing 'y' -> 'n' and 'Y' -> 'N' would keep the logic of the body of the if statement the same.
Also I noticed some of the game initialization statements need to be moved into the body of the do-while loop otherwise the user will be guessing the same random number for every game.
Last edited on
Topic archived. No new replies allowed.