How to make a program select a random number?

Mar 20, 2014 at 7:56am
Ok so I made this program and it is working just the way it is supposed to be. But I can't figure out that how to make it enter a random number, which the user will have to guess?

I'm doing this practice exercise.

Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.
If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.
The program must let the user continue to guess until the user correctly guesses the 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
 #include <iostream>

using namespace std;

int main()
{
    int number = 57;
    int guess;

    cout << "Guess the number from 1 and 100.\n";

    while(1)
    {
        cin >> guess;

        if (guess < 0)
        {cout << "Too low.\n";}

        else if
        (guess > 100)
        {cout << "Too high.\n";}

        else if (guess = number)
        {cout << "Your guess is correct.\n";}

        else
        {cout << "Your guess is incorrect.Please try again.\n";}
    }


    return 0;
}
Mar 20, 2014 at 9:48am
Mar 20, 2014 at 10:10am
To point some things:
1) You can and should use while(true). What you have done works fine but in C++ there are boolean values so you should use them.
2) You should output "too low" when the guess < number.
3) Similar as above, "too high" should be output when the guess > number.

To generate random numbers first include cstdlib (#include <cstdlib> ).
Then add srand(time(0)); just after int main(){. What this does is creates a random seed.
Finally do number = rand() % 100;. This will generate a random number between 0 and 100.

Edit: Typo.
Last edited on Mar 20, 2014 at 10:12am
Mar 20, 2014 at 10:19am
Why should he give ''too low' when the guess < number ? Since the range is 1-100.
Mar 20, 2014 at 10:41am
@ Last Banana: That's because it'll indicate whether the user is close to the number or not. If you put guess < 0 it'd be pretty pointless since it doesn't indicate anything except that the user is guessing out of range. The user can guess any number, you don't know what. If you just did if guess < 0, the probability of the user guessing right is 1%, which is pretty low. In the worst case scenario, the user would have to guess 99 times before he gets it right. At that point he'd be pretty pissed off ;). But, if you did guess < number, the probability of the user guessing right will increase each time he guesses wrong.
Last edited on Mar 20, 2014 at 10:48am
Mar 20, 2014 at 3:59pm
Thank you everyone for the help.

@Stormboy: I didn't knew about "rand" but now I know how to use it. So thank you for that.

So here is the program.

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

#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{

    int number = rand() % 100;
    int guess;

    cout << "Guess the number from 1 and 100.\n";

    while(true)
    {
        cin >> guess;

        if (guess < 0)
        {cout << "Too low.\n";}

        else if
        (guess > 100)
        {cout << "Too high.\n";}

        else if (guess == number)
        {cout << "Your guess is correct.\n";}

        else
        {cout << "Your guess is incorrect. Please try again.\n";}
    }


    return 0;
}
Mar 20, 2014 at 4:09pm
In the same program I'm required to print out the number of tries it took the user to guess the number.

This is what I could go so far. Cant figure out , how to make the code count the number to times user tried to guess the 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
#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{

    int number = rand() % 100;
    int guess;
    int tries;

    cout << "Guess the number from 1 and 100.\n";

    while(true)
    {
        cin >> guess;

        if (guess < 0)
        {cout << "Too low.\n";}

        else if
        (guess > 100)
        {cout << "Too high.\n";}

        else if (guess == number)
        {cout << "Your guess is correct.\n";}

        else
        {cout << "Your guess is incorrect. Please try again.\n";}
    }
    
    cout << "It took user" << tries << "tries to guess the number";
    


    return 0;
}
Mar 20, 2014 at 4:21pm
1. What should the value of 'tries' be at start, before the user has made any guesses?
2. What should happen to the value of 'tries' when the user makes one guess?
Topic archived. No new replies allowed.