Sign in to follow this  
jcwoodard

Guessing Game Code help

Recommended Posts

I have the code written but I need to know how to make it retain the guesses so when is says guess higher or lower it will change he range from 1-100 to the lowest and highest guesses. Basically keeping track ... if that makes any sense... any help would be appreciated. #include <stdio.h> #include <time.h> int main(void) { int secret, guess=-1, tries=0; // Seeds the random number generator. srand(time(0)); // Create the secret number. secret = 1 + rand()%100; // Keep on playing until the user has gotten the number. while (guess != secret) { // Get the current guess. printf("Enter your guess.(1 to 100)\n"); scanf("%d", &guess); // Print out the appropriate message for each choice. if (guess == secret) printf("You win!\n"); else if (guess < secret) {printf("You need to guess higher. Choose a number between %d and %d. Try again!\n", guess); tries++ ;} else {printf("You need to guess lower. Try again!/n"); tries++;} } printf("It took you %d attempts to guess the number.\n", tries); system ("pause"); }

Share this post


Link to post
Share on other sites
You will need a couple more variables, perhaps something like guessMin and guessMax to store the top and bottom of the range. Initialize them appropriately, and with each try, check if the guess is closer than one of your boundaries and update it as needed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this