Simple while loop does not terminate

Hello,

The only while loop in this batch of code will not terminate. Inside this while loop, there are multiple if-else statements. If the player correctly guesses the hiddenNumber, I assign the value of 0 to the variable guessAgain to attempt termination. Unfortunately, this assignment doesn't seem to actually happen as both the if-else and while functions continue to operate.

Any advice? I have limited knowledge with C++ as I only have a couple of days of studying done. The entire source code is shown below the source code excerpt.

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
  while (guessAgain = 1)
	{
		cout << playerOneName << ", please enter your guess between 0 and 100." << endl;
		cin >> playerOneGuess;
		if (playerOneGuess == hiddenNumber)	
		{		
			cout << "Congratulations, you win! You're much better than " << playerTwoName << "!" << endl;
			guessAgain = 0;
		}
		else if (playerOneGuess < hiddenNumber)
		{		
				cout << "Sorry, your guess of " << playerOneGuess << " was too low." << endl;
				guessAgain = 1;
		}
		else cout << "Sorry, your guess of " << playerOneGuess << " was too high." << endl;
			guessAgain = 1;

		if (guessAgain == 1)
		{	
			cout << playerTwoName << ", please enter your guess between 0 and 100." << endl;
			cin >> playerTwoGuess;
	
			if (playerTwoGuess == hiddenNumber)
			{
				cout << "Congratulations, you win! You're much better than " << playerOneName << "!" << endl;
				guessAgain = 0;
			}
			else if (playerTwoGuess < hiddenNumber)
			{
				cout << "Sorry, your guess of " << playerTwoGuess << " was too low." << endl;
				guessAgain = 1;
			}
			else cout << "Sorry, your guess of " << playerTwoGuess << " was too high." << endl;
				guessAgain = 1;
		}
		else cout << "Thanks for playing!" << endl;
	}


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main ()
{
	string playerOneName, playerTwoName;
	int numberOne, numberTwo;
	int hiddenNumber;
	int playerOneGuess, playerTwoGuess;
	int guessAgain;

	cout << "Welcome to the guessing game! \nIn this game for two, the first player to guess the hidden number wins." << endl;
	cout << "Player one, please enter your first name." << endl;
	cin >> playerOneName;
	cout << "Player two, please enter your first name." << endl;
	cin >> playerTwoName;
	cout << "Hello " << playerOneName << " and " << playerTwoName;
	cout << ". You both will now need to choose a private number. \nPlease select between 0 and 100." << endl;
	cout << playerOneName << ", please enter your private number." << endl;
	cin >> numberOne;
	cout << string (50, '\n');
	cout << playerTwoName << ", please enter your private number." << endl;
	cin >> numberTwo;
	cout << string (50, '\n');
	
	hiddenNumber = abs(numberTwo - numberOne);

	cout << "Now that you have both entered your private numbers, it's time to guess!" << endl;
	guessAgain = 1;

	while (guessAgain = 1)
	{
		cout << playerOneName << ", please enter your guess between 0 and 100." << endl;
		cin >> playerOneGuess;
		if (playerOneGuess == hiddenNumber)	
		{		
			cout << "Congratulations, you win! You're much better than " << playerTwoName << "!" << endl;
			guessAgain = 0;
		}
		else if (playerOneGuess < hiddenNumber)
		{		
				cout << "Sorry, your guess of " << playerOneGuess << " was too low." << endl;
				guessAgain = 1;
		}
		else cout << "Sorry, your guess of " << playerOneGuess << " was too high." << endl;
			guessAgain = 1;

		if (guessAgain == 1)
		{	
			cout << playerTwoName << ", please enter your guess between 0 and 100." << endl;
			cin >> playerTwoGuess;
	
			if (playerTwoGuess == hiddenNumber)
			{
				cout << "Congratulations, you win! You're much better than " << playerOneName << "!" << endl;
				guessAgain = 0;
			}
			else if (playerTwoGuess < hiddenNumber)
			{
				cout << "Sorry, your guess of " << playerTwoGuess << " was too low." << endl;
				guessAgain = 1;
			}
			else cout << "Sorry, your guess of " << playerTwoGuess << " was too high." << endl;
				guessAgain = 1;
		}
		else cout << "Thanks for playing!" << endl;
	}

	cout << "Thanks for playing!" << endl;

	return 0;
}
@VforValdes

Your problem lies in this line
while (guessAgain = 1)
A single equal sign, assigns a value to a variable, whereas, while (guessAgain == 1) would check to see if a variable is equal to what is being checked. So, no matter what the variable 'guessAgain' is changed to, the while assigns it to 1 again.
Thanks whitenite1 for your help.

I fixed the guessAgain = 1 error to guessAgain == 1. Unfortunately I still have the issue of both the while loop not terminating and the if loop not choosing else.

Any more insights? I feel like the variable guessAgain isn't being assigned to 0 as both functions, while and if, execute incorrectly.
An update on the code...

Since the variable guessAgain doesn't change from it's value of 1 unless playerOneGuess or playerTwoGuess == hiddenNumber, I removed the unnecessary guessAgain assignment statements that occur in the if-else functions when the number is incorrectly guessed. This has fixed my problem.

Any insight on why that fixed the issue?
Last edited on
@VforValdes

I left the program as it was, except for changing the while loop to two equal signs, and had no problem playing your game. So, no, I have no insights at this time. I did notice that after a win, you never ask if the players want to play another game, do the game keeps running with no way out unless you close the program. And, after each game, you never change the hidden number, so it's always the same. All in all, though, it's coming out quite good. It was fun to play, even against myself. I hope you expand the game, to allow the quit option, and also add in a scoring variable. Keep track of how many wins and losses each player accumulates and declare a winner based on wins.
Thanks for the encouragement whitenite1.

The game was a learning experience for me and I will likely make another game in the near future. Implimenting your ideas by adding in the extra features for ease of replayability and more functionability will be important then. Again, thanks for the input!
Topic archived. No new replies allowed.