So Ive been trying to write a program that guesses a number 100-999 within 10 tries. I cant figure out how to write the tries remaining along with if the number is higher or lower than the guess. It needs to count down, most examples im finding are just showing the number of tries in which they guessed the number.
do {
// generate random number between 100 - 999
number = (rand() % (999 - 100 + 1)) + 100;
cout << "You have 10 tries!\n";
cout << "Guess the number (100-999): ";
for (int tries = 0; tries < 10; tries--) {
cin >> guess;
// validate users input
if ((guess < 100) || (guess > 999)) {
cout << endl << "You must guess a number between 100 - 999. Guess again: ";
tries++;
}
I'm really lost as to where to take this! please help!
You do it just like your validation..
if(guess == number) //win
win code;
else if(guess < number) //
//less than code
else //greater than code
tries is being modified by the loop and inside the loop.
That is wrong, just do one or the other.
to count down:
for(tries = 10; tries; --tries)
and print how many tries they have left
or count up and say how many they have used, whatever on that...
its boring, but the binary search ensures you can guess in just a few tries ... you cut the number of options in half each time...