random number

Hello !

I'm knew here , and having a problem .

I'm trying to make program that picks numbers between 1 and 100 , and lets the user guess what the number is .


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

#include <cstdlib>

#include <ctime>

#include <iostream>



using namespace std;



int main()

{
 int t,guessing ;

cin>>guessing;
 srand(time(NULL));

for(int i = 1; i < 100; ++i ) {

t=rand;
cout<< t<<endl;

if (guessing>t){
cout <<"too high";
}

}


}


when I try to build it , it always says (invalid conversion from int to int )

what should I do to solve this problem ?

Last edited on
To call rand you have to put parenthesis after it. rand()
rand() will return a number between 0 and RAND_MAX. To get a random number between 1 and 100: rand() % 100 + 1;
thank you very much peter & dash

it works fine now


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
#include <cstdlib>

#include <ctime>

#include <iostream>



using namespace std;



int main()

{
 int t,guessing ;

cin>>guessing;
 srand(time(NULL));



t=rand() % 100 + 1;
cout<< t<<endl;

if (guessing>t){
cout <<"too high";
}
 else  if (guessing<t){
cout <<"too low";


}

else if (guessing==t){
cout <<"right";


}



}


---------------------------------

% 100 + 1;

but why do we have to put +1

what the use of it
the function rand() as mentioned above generates a number between 0 and RAND_MAX. By changing it to rand() % 100, 100 is used instead of RAND_MAX thus the number generated will vary between 0 to 99 (100 numbers). So you add 1 to the result so that it's 1 to 100.
Last edited on
OH it starts from 0 not 1

thank you I appreciate your help
Last edited on
Topic archived. No new replies allowed.