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?
19 Réponses
+ 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
+ 5
If my math is correct, 100+1-1 = 100
+ 5
He specifically asked how to generate a random number between 1 and 100. I told him how to do it. Case closed 😶
+ 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
+ 4
Anna
What about different ranges like so [10, 100]
10 + rand() % (100 + 1 - 10);
+ 4
Yes, or different programming languages
+ 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. ;)
+ 3
Anna LOL!
+ 2
Start by showing the code.
+ 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.
+ 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.
+ 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
+ 1
The code would only as for input once on SL.
A compiler is needed to have multiple guesses.
0
sorry
what do u mean
0
what does the code mean?
0
no
i mean
it says "Looks like your program need input"
0
i mean
why cant i input during processing the code
0
oh thx
so SoloLearn may make changes for input
0
ok thx for all