I have finished my code for a program that allows a user to enter a range of values to guess from, then randomly generates the number, and then allows the user to guess and reports too low, too high, or correct. When I test run the program, I only get outputs of "Too high." and I've tried debugging and looking at my code but I can't see where it went wrong. Any help is much appreciated!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int min, max, number, guess, count = 0;
printf("Please enter two integers for the range:
");
scanf("%d %d", &min, &max);
number = (min + rand()%(max - min + 1));
printf("I'm thinking of a number between %d and %d.
", min, max);
printf("Enter your guess:
");
scanf("%d", &guess);
while(guess != number)
{
if(guess < number)
{
count++;
printf("Too low.
");
printf("Enter your guess:
");
scanf("%d", &guess);
}
else
{
count++;
printf("Too high.
");
printf("Enter your guess:
");
scanf("%d", &guess);
}
}
count++;
printf("Correct, it took you %d tries to guess my number. ", count);
return 0;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…