| freedom12 (8) | |||
|
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 .
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
|
|||
| Peter87 (3691) | |
To call rand you have to put parenthesis after it. rand()
| |
|
|
|
| Dash (122) | |
rand() will return a number between 0 and RAND_MAX. To get a random number between 1 and 100: rand() % 100 + 1;
| |
|
|
|
| freedom12 (8) | |||
|
thank you very much peter & dash it works fine now
--------------------------------- % 100 + 1;but why do we have to put +1what the use of it | |||
|
|
|||
| Maniax (36) | |
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
|
|
| freedom12 (8) | |
|
OH it starts from 0 not 1 thank you I appreciate your help | |
|
Last edited on
|
|