Check this, maybe?

This is my task:

Write a program that generates a random number and asks the user to guess what the
number is. If the user’s guess is higher than the random number, the program should
display “Too high, try again.” If the user’s guess is lower than the random number,
the program should display “Too low, try again.” The program should use a loop that
repeats until the user correctly guesses the random number.

Can anyone check my code for me and give me some advice on what I could add or change? Or What I could have possibly done wrong?

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    const int MIN_VALUE = 1;
    const int MAX_VALUE = 5;

    int randNum;
    int num;

    unsigned seed = time(0);

    cout << "Random Number Guessing Game\n" << endl;
    randNum = (rand() % (MAX_VALUE - MIN_VALUE +1)) + MIN;

    while(randNum >= 1 && randNum <= 5)
       {
            cout << "Guess the number" << endl;
            cin >> num;
            if(num == randNum)
            {
                cout << "congrats you guessed correctly!" << endl;
            }
            if(num > randNum)
            {
                cout << "Too high, try again." << endl;
                cin >> num;
            }
            if(num < randNum)
            {
                cout << "Too low, try again." << endl;
                cin >> num;
            }
            /*if(num < 1 && num > 100)
                cout << "You are out of the numeric range!" << endl;
                cout << "Try again within the range." << endl;
                cin >> num;*/
       }

    return 0;
}
Last edited on
Line 15: Use the seed you generated. Call srand(seed);

Line 19: You already have symbolic constants MIN_VALUE and MAX_VALUE. Use those instead of putting numeric literals (1 and 5) in your range check. On second thought, this range check is pointless. You're looping until the user guesses the correct number. randNum isn't changing in the loop, and it doesn't matter if it is between 1 and 5 anyway. You can either check to see if the user's guess matches the generated number, or you can make this an infinite loop that you have to break from manually. [ while(true) ]

Line 25: The right number has been guessed. This would be a good time to break; out of the while loop if you've decided to use while(true)

Lines 30 and 35: No need to get input here, you'll be asking for and receiving new input on lines 21 and 22.
Last edited on
Okay so I revised the code based on your input. One thing I noticed though is that, when getting a random number it's only choosing 2 or 4. Is it because the range is too narrow? Initially I did 1-100 but I changed it to 1-5 to check if I could actually guess the correct number.

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

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    const int MIN_VALUE = 1;
    const int MAX_VALUE = 5;

    int randNum;
    int num;

    unsigned seed = time(0);

    srand(seed);

    cout << "Random Number Guessing Game\n" << endl;
    randNum = (rand() % (MAX_VALUE - MIN_VALUE +1)) + MIN_VALUE;

    while(randNum >= MIN_VALUE && randNum <= MAX_VALUE)
       {
            cout << "Guess the number" << endl;
            cin >> num;
            if(num == randNum)
            {
                cout << "congrats you guessed correctly!" << endl;
                break;
            }
            if(num > randNum)
            {
                cout << "Too high, try again." << endl;
                //cin >> num;
            }
            if(num < randNum)
            {
                cout << "Too low, try again." << endl;
                //cin >> num;
            }
            /*if(num < 1 && num > 100)
                cout << "You are out of the numeric range!" << endl;
                cout << "Try again within the range." << endl;
                cin >> num;*/
       }

    return 0;
}

Last edited on
You're still doing a useless range check on line 22. Replace with while(true)

As far as not getting all the numbers you expect, how many times did you actually run the program? Try this. Replace line 20 with this code:
1
2
3
4
5
    for(int i = 0; i < 100 ; i++)
    {
        randNum = (rand() % (MAX_VALUE - MIN_VALUE +1)) + MIN_VALUE;
        std::cout << randNum << ' ';
    }
This is the same code that you had before, except now it runs 100 times. It generates a bunch of random numbers and prints them, keeping only the last one. This should be enough to convince you that you're getting numbers in your expected range.
I ran it countless times when the values were 1-5 but now I have changed them back to 1-100 and it seems to be working just how I would like it to. I get different random numbers now every time it runs.

Here is I guess the final revision, I don't think the last bit you said to change is necessary anymore.

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

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    const int MIN_VALUE = 1;
    const int MAX_VALUE = 100;

    int randNum;
    int num;

    unsigned seed = time(0);

    srand(seed);

    cout << "Random Number Guessing Game\n" << endl;
    randNum = (rand() % (MAX_VALUE - MIN_VALUE +1)) + MIN_VALUE;

    while(true)
       {
            cout << "Guess the number" << endl;
            cin >> num;
            if(num == randNum)
            {
                cout << "congrats you guessed correctly!" << endl;
                break;
            }
            if(num > randNum)
            {
                cout << "Too high, try again." << endl;
                //cin >> num;
            }
            if(num < randNum)
            {
                cout << "Too low, try again." << endl;
                //cin >> num;
            }
            /*if(num < 1 && num > 100)
                cout << "You are out of the numeric range!" << endl;
                cout << "Try again within the range." << endl;
                cin >> num;*/
       }

    return 0;
}



I just need to remove what I commented out and put some comments on what does what and I'm done. Thank you @booradley60! :)
Last edited on
Topic archived. No new replies allowed.