Random Number Generator

Hi.

I want to program a program that produces a random number between 1-10, then if the number is correct, says like HEY GOOD JOB and if its not says try AGAIN! also, at any point the user can quit by typing in X.

I know that I can use stuff like cout << "Hey put in a guess now" to prompt the user but I dont know how to accept inputs.
For example, how do I use scanf to do this?
I know that if I use scanf, it takes in a value, but where the heck does it store it?

eg. scanf(%s,guess);
Is that valid? I get an invalid expression error when trying to use that in C++.
If you
know
why don't you try to do it?
closed account (N36fSL3A)
You can get a random number 0 through 10 like this.
1
2
3
4
5
6
7
8
9
10
11
#include <ctime>

int main()
{
    srand(time(NULL)); // Seed the time
    unsigned int val = rand() % 10;

    // Do whatever you want with the variable 'val'.
    
    return 0;
};


If you want 10 values, do this
1
2
3
4
5
6
7
8
9
10
11
#include <ctime>

int main()
{
    srand(time(NULL)); // Seed the time
    unsigned int val = rand() % 9

    // Do whatever you want with the variable 'val'.
    
    return 0;
};
Last edited on
Lumpkin wrote:
You can get a random number 0 through 10 9 like this.


Lumpkin wrote:
If you want 10 1 values, do this


Fixed.

[Edit: You also need to #include <cstdlib> , and you should be using std::srand, std::rand and std::time.]
Last edited on
Topic archived. No new replies allowed.