Number Guessing Game

I'm trying to write a number guessing game but for some reason, it will not continue to ask you to write in a number. Can anyone explain?

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{

    int num; 
    int guess; 
    bool isGuessed;
    srand(time(0));
    num = rand() % 100;
    isGuessed = false;
    while (!isGuessed)
    {cout<<"\t\t\tWelcome to the High-Low Game!\n\n"
    <<"\tGame Rules:\n\t\tThe computer randomly chooses a number between 1 and 100."
    <<"\n\t\tThe player must guess that number.\n\t\tEach time the player makes a guess, the compurer"
    <<"\n\t\tcan give one of three answers:"
    <<"\n\t\tHIGH. Guess again. (If the guess is higher than the computer's number.)."
    <<"\n\t\tLOW. Guess again. (If the guess is lower than the computer's number.)"
    <<"\n\t\tCORRECT!! (If the player correctly guesses the number.)"
    <<"\n\n\tOther:\n\t\tThe computer keeps track of the number of guesses"
    <<"\n\t\tthe player takes to guess the number.\n\n";
    
    system ("pause");
    system ("cls");
    
    break;}
{
    cout << "\tEnter your guess <1-100>:";
    cin >> guess;
    if (guess == num)  
    {
    cout << "CORRECT!!\n\n"; 
    isGuessed = true;
    }
     
    else if (guess < num) 
    cout << "\t\t\t\tLOW. Guess again.\n\n";
    
    else if (guess > num)
    cout << "\t\t\t\tHIGH. Guess again.\n\n";
} 
    system ("pause");
    return 0;
}
It will only loop through because that is exactly what you tell it to do.

The only while loop you have you break out of on the first run. Though granted it wouldn't loop through the whole guessing part as it is anyways.

Here is your while loop and the break I am talking about. I cleaned it up a bit specifically the bracket placements. It is very important to keep your brackets placed where they can be easily seen and matched to one another.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while (!isGuessed)
{
    cout<<"\t\t\tWelcome to the High-Low Game!\n\n"
        <<"\tGame Rules:\n\t\tThe computer randomly chooses a number between 1 and 100."
        <<"\n\t\tThe player must guess that number.\n\t\tEach time the player makes a guess, the compurer"
        <<"\n\t\tcan give one of three answers:"
        <<"\n\t\tHIGH. Guess again. (If the guess is higher than the computer's number.)."
        <<"\n\t\tLOW. Guess again. (If the guess is lower than the computer's number.)"
        <<"\n\t\tCORRECT!! (If the player correctly guesses the number.)"
        <<"\n\n\tOther:\n\t\tThe computer keeps track of the number of guesses"
        <<"\n\t\tthe player takes to guess the number.\n\n";
    
    system ("pause");
    system ("cls");
    
    // Here you break out of your while loop (IE you stop it from continuing to loop).
    break;
}


So that code will run once and then break out of the loop. Once it breaks out of the loop it will execute the next statement that is right after the loop which would be cout << "\tEnter your guess <1-100>:"; on line 30.

It will then run everything from then until the return statement at the bottom which exits your program.

So the reason why it is only running once is because you aren't looping through anything. I have a feeling that since there is other brackets in your code that don't match up to anything that you assumed everything under the while statement was part of the while loop but it isn't. Again it is very important to keep your brackets visible and clearly aligned.

So what you need to do is get rid of that break statement and include this inside of your while loop brackets.

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "\tEnter your guess <1-100>:";
    cin >> guess;
    if (guess == num)  
    {
    cout << "CORRECT!!\n\n"; 
    isGuessed = true;
    }
     
    else if (guess < num) 
    cout << "\t\t\t\tLOW. Guess again.\n\n";
    
    else if (guess > num)
    cout << "\t\t\t\tHIGH. Guess again.\n\n";

Topic archived. No new replies allowed.