Ok so I made this program and it is working just the way it is supposed to be. But I can't figure out that how to make it enter a random number, which the user will have to guess?
I'm doing this practice exercise.
Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.
If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.
The program must let the user continue to guess until the user correctly guesses the number.
To point some things:
1) You can and should use while(true). What you have done works fine but in C++ there are boolean values so you should use them.
2) You should output "too low" when the guess < number.
3) Similar as above, "too high" should be output when the guess > number.
To generate random numbers first include cstdlib (#include <cstdlib> ).
Then add srand(time(0)); just after int main(){. What this does is creates a random seed.
Finally do number = rand() % 100;. This will generate a random number between 0 and 100.
@ Last Banana: That's because it'll indicate whether the user is close to the number or not. If you put guess < 0 it'd be pretty pointless since it doesn't indicate anything except that the user is guessing out of range. The user can guess any number, you don't know what. If you just did if guess < 0, the probability of the user guessing right is 1%, which is pretty low. In the worst case scenario, the user would have to guess 99 times before he gets it right. At that point he'd be pretty pissed off ;). But, if you did guess < number, the probability of the user guessing right will increase each time he guesses wrong.
1. What should the value of 'tries' be at start, before the user has made any guesses?
2. What should happen to the value of 'tries' when the user makes one guess?