Jump to content
  • Advertisement
Sign in to follow this  
jcwoodard

Guessing Game Code help

This topic is 4060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!