while loop age guess

Write a C++ program that uses a while loop to guess a persons age, ask the user if the want to try again if no end the loop. Of course if they guess it the loop also ends
use the rand function to make a guess given a certain range. First you make a guess using rand. Say the guess is between 1 and 130. You will then ask the user if the guess you made is higher or lower than their age depending on what they respond, you will then use rand again to make a guess using the previous value you guessed as a higher or lower range and you keep doing this until the computer "guesses" the age
this is what i've got so far but when i type N for no it doesn't guess again

{

srand(time(NULL));

char input = 'Y';

while(input == 'Y')
{

int guess = (rand() % 100) + 9; //guess from range of 0-100

printf("Since you can use a computer i'd wager your %d years old\n", guess);

printf("If I got your age wrong I can try again (y/n): ");

input = getchar();

toupper(input);


}
}
int guess = (rand() % 100) + 9; //guess from range of 0-100

Is a random number between 9 and 109 (9 and 109 not included)

If you want to make a guess between 0 and 100 (really should be 1 and 100), it should be just rand() % 100 (1 and 100 is rand() % 100 + 1


toupper(input);
Should be input = toupper(input);

As a final note, the aim of this is to make your guess as close the age as possible for each iteration of the while loop. If you keep guessing random numbers between 0 and 100, it will take a while before you finally guess the right age - it is possible, but will be very boring for the user interacting with it. A way to make it faster is to ask the user how close your guess is to their age and adjust the range of guesses by what the user enters.
Last edited on
Topic archived. No new replies allowed.