Aug 31, 2012 at 9:26am UTC
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 Aug 31, 2012 at 9:29am UTC
Aug 31, 2012 at 9:30am UTC
To call rand you have to put parenthesis after it. rand()
Aug 31, 2012 at 11:06am UTC
rand()
will return a number between 0 and RAND_MAX. To get a random number between 1 and 100: rand() % 100 + 1;
Aug 31, 2012 at 1:03pm UTC
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 Aug 31, 2012 at 1:04pm UTC
Aug 31, 2012 at 1:12pm UTC
OH it starts from 0 not 1
thank you I appreciate your help
Last edited on Aug 31, 2012 at 1:13pm UTC