Guess My Number Problem

In this Guess My Number Program, you have 20 tries to try and get the number 1-500. It tells you if its bigger or smaller. Anyway, I run it and I enter a random number. But regardless of what I enter, it says it's bigger or smaller (fine with that) but then it goes to saying "Congratulations, you have won. Blah blah blah".
I can keep entering numbers but it'll just keep congratulating me even if I didn't get it correct.
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
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    restart:
    int number;
    int guess;
    int tries;
    char response;
    
    srand(time(0));
    number = rand() % 500 + 1;
    
    cout << "I have generated a number. Attempt to guess it! (1-500)" << endl;
    cout << "Remember - you have only twenty tries!" << endl;
    cin >> guess;
    if (guess > number)
    {
              cout << "Your guess is too big." << endl;
              tries++;
    }
    else if (guess < number)
    {
              cout << "Your guess is too small." << endl;
              tries++;
    }
    if (tries == 20)
    {
              cout << "Sorry! That was your twentieth try! Want to restart the game? Press 'Y' or 'y' if you do" << endl;
              cin >> response;
              if (response != 'Y' || response != 'y')
              {
              goto restart;
              }
              else
              {
              system("CLS");
              return(0);
              }
    }
    
    if (guess == number && tries <= 20);
    {
              cout << "Congratulations! You guessed the number in " << tries << " tries!" << endl;
              cout << "Want to restart the game? Press 'Y' or 'y' if you do." << endl;
              cin >> response;
              if (response != 'Y' || response != 'y')
              {
              goto restart;
              }
              else
              {
              return(0);
              }
    }
}


Thanks for any help!
Last edited on
From what I can see the problem looks like the last if line 44, your tries >= 20 is meant to be <= 20.

Line 31 is meant to say twentieth.
Thanks. I fixed the spelling errors and stuff. The problem still exists. Here's an example of what I inputted.

"I have generated a number. Attempt to guess it! (1-500)"
"Remember - you have only twenty tries!"
"12"
"Your guess is too small"
"Congratulations! You guessed the number in 3 tries!"
"Want to restart the game? Press 'Y' or 'y' if you do."

You'll need to add a loop, right now the programs just going straight to the finish.
Make it loop 20 times, when the guess is correct it can break the loop to the congratulations message otherwise do the failure thing.
Try a do while loop
I can't get it working correctly even with a do-while loop. I must be getting it horribly wrong. Here's my second attempt.
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
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    restart:
    int number;
    int guess;
    int tries;
    char response;
    
    srand(time(0));
    number = rand() % 500 + 1;
    
    cout << "I have generated a number. Attempt to guess it! (1-500)" << endl;
    cout << "Remember - you have only twenty tries!" << endl;
    cin >> guess;
    if (guess > number)
    {
              cout << "Your guess is too big." << endl;
              tries++;
    }
    else if (guess < number)
    {
              cout << "Your guess is too small." << endl;
              tries++;
    }
    if (tries == 20)
    {
              cout << "Sorry! That was your twentieth try! Want to restart the game? Press 'Y' or 'y' if you do" << endl;
              cin >> response;
              if (response != 'Y' || response != 'y')
              {
              system("CLS");
              goto restart;
              }
              else
              {
              return(0);
              }
    }
    
    if (guess == number && tries <= 20);
    {
              cout << "Congratulations! You guessed the number in " << tries << " tries!" << endl;
              cout << "Want to restart the game? Press 'Y' or 'y' if you do." << endl;
              cin >> response;
              if (response != 'Y' || response != 'y')
              {
              system("CLS");
              goto restart;
              }
              else
              {
              return(0);
              }
    }
}
Last edited on
Umz made a very good point. It would be good to use a 'do...while' loop.

I noted, however, that your variable 'tries' hasn't been initialized. It's only been declared. Perhaps that may be the problem?


By the way, your program is nicely written. very neat and easy to read (unlike others that i've seen).
Last edited on
Here is my do-while loop attempt. I don't really understand how it works so that must be where my problem lies. I think the problem now is when the do-while loop activates, it keeps looping until tries is 20 and not going back and asking for another guess.
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
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    restart:
    int number;
    int guess;
    int tries;
    char response;
    
    srand(time(0));
    number = rand() % 500 + 1;
    tries = 0;
    
    cout << "I have generated a number. Attempt to guess it! (1-500)" << endl;
    cout << "Remember - you have only twenty tries!" << endl;
    cin >> guess;
    do
    {
        cout << "Your number is too big! Try again." << endl;
        tries++;
    } while (guess > number && tries < 20);
    do
    {
        cout << "Your number is too small! Try again." << endl;
        tries++;
    } while (guess < number && tries < 20);
    if (tries == 20)
    {
        cout << "Sorry! That was your twentieth try. Game Over!" << endl;
        cout << "The number was " << number << "." << endl;
        cout << "Try again? (Y/N)" << endl;
        cin >> response;
        if (response != 'Y' || response != 'y')
        {
                     return(0);
        }
        else
        {
                     system("CLS");
                     goto restart;
        }
    }
    if (guess == number && tries <= 20)
    {
        cout << "Congratulations! You guessed the number in " << tries << " tries!" << endl;
        cout << "Want to restart the game? Press 'Y' or 'y' if you do." << endl;
        cin >> response;
        if (response != 'Y' || response != 'y')
        {
                     return(0);
        }
        else
        {
                     system("CLS");
                     goto restart;
        }
    }
}


P.S. Thanks! No wonder - I forgot to even declare what tries was in the first place!
As far as what i understand, this program would only need just a single 'do...while' loop. I'll leave that to you to figure that out :)

The 'if' statement could probably just be used only for determining the guessed number.

From what I understand of your program, when a person wants to restart the game, tries will automatically reset its value.
Last edited on
Topic archived. No new replies allowed.