My while loop won't do anything after I input the word yes

My while loop won't do anything after I input the word yes. I have marked the line that is giving me problems. Please help!

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  #include <iostream>
#include <string>

using namespace std;


void introduction(int high, int low);
int getGuess();
bool testGuess(int guess, int numberToGuess);



int main(int argc, char* argv[])
{
	int high = 10;
	int low = 1;

	int numberToGuess;
	bool stillPlaying = true;
	bool winner = false;

	int guess;

	while (stillPlaying)
	{
		// generate a random number between low and high value
		numberToGuess = rand() % (high - low + 1) + low;

		//tell the user about the game
		introduction(high, low);

		while (!winner)
		{
			guess = getGuess();
			int NumberofGuesses = 1;

			winner = testGuess(guess, numberToGuess);
			if (winner)
			{
				cout << "Congatulations, You WON!!!" << endl;
			}
			else 
			{
				cout << "You have guessed " << NumberofGuesses << " times" << endl;
				++NumberofGuesses;
			}
			//output the number of guesses they've made so far
			
				cout << "Would you like to play again? (Yes or No)" << endl;
				string replay;
				cin >> replay;
				bool questionAnswered = false;
				while (!questionAnswered) //From here to...
				{
					if (replay == "Yes" || replay == "yes")
					{
						stillPlaying = true;
						bool questionAnswered = true;
					}
					else if (replay == "No" || replay == "no")
					{
						stillPlaying = false;
						bool questionAnswered = true;
						return 0;
					}
				} // Here
		} 
	}

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


	cin.ignore();
	cin.get();

}





//Tells the user the rules of the game
void introduction(int high, int low)
{
	cout << "I'm thinking of a number between 1 and 10..." << endl;
}

//Prompts for, inputs, and returns the next guess
int getGuess()
{
	string Guess;
	cout << "What is your guess?" << endl;
	cin >> Guess;
	return 1;
}

// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
	if (guess != numberToGuess)
	{
		cout << "I am sorry, that is not correct." << endl;
		return false;
	}
	if (guess = numberToGuess)
	{
		cout << "That is correct!" << endl;
		return true;
	}
	return (main);
}
Last edited on
Hi

Line 106. = is assignment, not equality test. This could be replaced with else

Delete line 111, it is nonsense.

Explain to us the logic behinds lines 53 to 66. Doing this might help you see the problem.

With yes no answers, restrict them to Y or N (don't get involved in all the permutations of their spelling) use std::toupper to convert y or n to uppercase. Then your tests will be easier.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (!questionAnswered) //While the question is not answered, it will:
				{
					if (replay == "Yes" || replay == "yes") // if the answer to the question is yes then:
					{
						stillPlaying = true; // You are still playing
						bool questionAnswered = true; //  and the question has been answered so it should get out of the loop
					}
					else if (replay == "No" || replay == "no")  // if the answer to the question is no then:
					{
						stillPlaying = false; // You are not still playing
						bool questionAnswered = true; //  and the question has been answered so it should get out of the loop
						return 0;
					}
				}
This might be another way to go, but it still doesn't work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << "Would you like to play again? (Y or N)" << endl;
			cin >> replay;
			while (stillPlaying = true)
			{
				if (replay == "Y" || replay == "y")
				{
					stillPlaying = true;
				}
				if (replay == "N" || replay == "n")
				{
					return 0;
				}
				else
				{
					cout << "I'm sorry, I don't understand." << endl;
				}
There was 1 line you didn't comment.

Have you compiled this, there are warnings? Post them here.
The return 0 line ends the program and there are no warnings, it just stops without exiting out of the program.
Also learn to use a debugger. Keep an eye on the variable values as you step through 1 line at a time.

Running the program shows deficiency's.

there are no warnings,


Set your compiler so it shows warnings. One can run the code in cpp.sh using the Edit& Run link
The one warning,
incorrect operator: assignment of constant in Boolean context. Consider using '==' instead.
Has been fixed and it is doing the same thing as before.
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
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <string>

using namespace std;

void introduction(int high, int low);
int getGuess();
bool testGuess(int guess, int numberToGuess);

int main(int argc, char* argv[])
{
    int high = 10;
    int low = 1;
    
    int numberToGuess;
    
    bool winner = false;
    
    int guess;
    
    char answer{'Y'}; // <---
    while (toupper(answer) == 'Y' && winner == false)
    {
        numberToGuess = rand() % (high - low + 1) + low;
        
        //tell the user about the game
        introduction(high, low);
        
        guess = getGuess();
        int NumberofGuesses = 1;
        
        winner = testGuess(guess, numberToGuess);
        if (winner)
        {
            cout << "Congatulations, You WON!!!" << endl;
            break; // <--
        }
        else
        {
            cout << "You have guessed " << NumberofGuesses << " times" << endl;
            ++NumberofGuesses;
            // TODO: NEED TO TEST AGAINST NO. GUESSES
        }
        
        cout << "Would you like to play again? (Y or N): ";
        cin >> answer;
    }
    
    cout << "Thanks for playing!" << endl;
    
    return 0; // <--
}

//Tells the user the rules of the game
void introduction(int high, int low)
{
    cout << "I'm thinking of a number between 1 and 10..." << endl;
}

//Prompts for, inputs, and returns the next guess
int getGuess()
{
    string Guess;
    cout << "What is your guess? ";
    cin >> Guess;
    return 1;
}

// returns true if guess is correct
//if guess is not correct, outputs a high or low message and returns false
bool testGuess(int guess, int numberToGuess)
{
    if (guess != numberToGuess)
    {
        cout << "I am sorry, that is not correct." << endl;
        return false;
    }
    if (guess == numberToGuess)// <--
    {
        cout << "That is correct!" << endl;
        return true;
    }
    return 0; // <--
}


I'm thinking of a number between 1 and 10...
What is your guess? 5
I am sorry, that is not correct.
You have guessed 1 times
Would you like to play again? (Y or N): y
I'm thinking of a number between 1 and 10...
What is your guess? 7
I am sorry, that is not correct.
You have guessed 1 times
Would you like to play again? (Y or N): n
Thanks for playing!
Program ended with exit code: 0
Thank you guys for the comments, they were very helpful!
Topic archived. No new replies allowed.