[Solved] Why do I really have to input first? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[Solved] Why do I really have to input first?

I made a cpp programme to guess a random number. But the inputs affect outputs. What can I do?

27th Oct 2018, 3:55 AM
デジモン Lover
デジモン Lover - avatar
19 Answers
+ 5
srand(time(NULL)); should be used only once. You're using it in a loop. The loop isn't really needed. If you want to generate a random number between 1 and 100, use rand() % 100 + 1. The main problem is that in line 20, you need to cin guess, not number. https://code.sololearn.com/cqtqs8OooZk0/?ref=app
27th Oct 2018, 5:04 AM
Anna
Anna - avatar
+ 5
If my math is correct, 100+1-1 = 100
27th Oct 2018, 6:59 AM
Anna
Anna - avatar
+ 5
He specifically asked how to generate a random number between 1 and 100. I told him how to do it. Case closed 😶
27th Oct 2018, 9:07 AM
Anna
Anna - avatar
+ 4
Anna " [...] If you want to generate a random number between 1 and 100, use rand() % 100 + 1." lower bound + rand() % ( upper band + 1 - lower band) 1 + rand() % ( 100 + 1 - 1 ) _____ https://en.cppreference.com/w/c/numeric/random/rand
27th Oct 2018, 6:40 AM
Babak
Babak - avatar
+ 4
Anna What about different ranges like so [10, 100] 10 + rand() % (100 + 1 - 10);
27th Oct 2018, 8:17 AM
Babak
Babak - avatar
+ 4
Yes, or different programming languages
27th Oct 2018, 8:41 AM
Anna
Anna - avatar
+ 3
Anna The question's explicitly tagged "cpp" and also the OP's code is in C++, as well. So, talking about the nature of randomness and underlying mechanic in a different language is out of the question, here. Maybe we could argue over that in a different post, or perhaps a different forum. ;)
27th Oct 2018, 8:47 AM
Babak
Babak - avatar
+ 3
Anna LOL!
27th Oct 2018, 9:39 AM
Babak
Babak - avatar
+ 2
Start by showing the code.
27th Oct 2018, 4:13 AM
Manual
Manual - avatar
+ 2
SSGSS_Goku Anna is right, you want to use time(0) once. SL will give you one time value and making every number random after srand(time(0)) the same.
27th Oct 2018, 5:47 AM
Manual
Manual - avatar
+ 2
SSGSS_Goku Perhase in the future. You can ask at info@sololearn.com. - Also the best answer does nothing for the user asking the question. Please check Anna 's answer.
27th Oct 2018, 5:51 AM
Manual
Manual - avatar
+ 1
SSGSS_Goku This is a code. One I am showing you. Linking makes it easy for others to help solve issue and debug. https://code.sololearn.com/c4yx0UjmCnBS/?ref=app
27th Oct 2018, 4:23 AM
Manual
Manual - avatar
+ 1
The code would only as for input once on SL. A compiler is needed to have multiple guesses.
27th Oct 2018, 5:43 AM
Manual
Manual - avatar
0
sorry what do u mean
27th Oct 2018, 4:14 AM
デジモン Lover
デジモン Lover - avatar
0
what does the code mean?
27th Oct 2018, 4:16 AM
デジモン Lover
デジモン Lover - avatar
0
no i mean it says "Looks like your program need input"
27th Oct 2018, 5:38 AM
デジモン Lover
デジモン Lover - avatar
0
i mean why cant i input during processing the code
27th Oct 2018, 5:43 AM
デジモン Lover
デジモン Lover - avatar
0
oh thx so SoloLearn may make changes for input
27th Oct 2018, 5:46 AM
デジモン Lover
デジモン Lover - avatar
0
ok thx for all
27th Oct 2018, 5:49 AM
デジモン Lover
デジモン Lover - avatar