"Play again" loop acts like it works but doesn't

Hey guys and gals, just wondering if I can get some help with this. I'm a beginner and working on a game that asks if you want to play again after you're done. As it stands, it will ask if I want to play again, and prompt the first question in the game (Guesses a number between 1-100, always starts with 50, and asks if the user's number is higher, lower, or correct), but then the program stalls after that. It always runs fine the first time through, it's only when the loop repeats that I have trouble. Any help is much appreciated!!!


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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>


using namespace std;

int main () {

int number_guess;
int min;
int max;
string guess;
string play_again = "y";
    
while (play_again == "y")
{
number_guess = 50;
min = 1;
max = 101;
guess = "";

    
        cout << "Think of a number between 1 and 100." << endl;
        cout << "Is it " << number_guess << "? (h/l/c): ";
        getline (cin, guess);
        while (guess != "c")
        {
            // If too low
            if (guess == "h")
            {
                min = number_guess;
                number_guess = (max + min)/2;
                cout << "Is it " << number_guess << "? (h/l/c): ";
                cin >> guess;
            }
            
            // If too high
            else if (guess == "l")
            {
                max = number_guess;
                number_guess = (max + min)/2;
                cout << "Is it " << number_guess << "? (h/l/c): ";
                cin >> guess;
            }
            else if (guess == "c")
            {
                cout << "Yay!" << endl;
                break;
            }
        }
        
    cout << "Do you want to play again? (y/n): ";
    cin >> play_again;
}

return 0;
}
change getline (cin, guess); to getline (cin>>ws, guess); or to cin>>guess;

when you do cin >> play_again; you'll capture "y" but leave an '\n' in th e input buffer. Then comes the getline() that reads until a line break, so `guess' ends up being empty and you've got an endless loop.
cin>>ws will drop all the whitespace before performing the reading.

By the way, the condition on line 47 will always fail, ¿can you realise why?
Thank you, that fixed it! Is it the break statement? You're right, it would never read out the "Yay" even when the number was right, so I took out that whole elif statement and just moved the "yay" down out of the loop above the "Do you want to play again" line, because I figured if you'd gotten to that point your number was right anyway. I'm really, really new to this so I'm not exactly sure why line 47 was breaking everything.

Thanks again!
It's not because of the break statement but that's a good guess.

Hint: Look at your Loops
Last edited on
Topic archived. No new replies allowed.