-
Number guessing.
Hi Folks
I have the following number guessing code. Using averages I want to narrow down the numbers but can't figure out how to narrow the window each time the loop runs.
Any help would be appreciated!
Code:
#include <stdio.h>
int main(void)
{
int start = 50;
int min = 1;
int max = 100;
long int hg = (start + max) / 2; //high guess
long int lg = (start + min) / 2; //low guess
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a Y if my guess is right, an H if it's higher or \n");
printf("an L if it's lower.\n");
printf("\n");
printf("Is your number 50?: ");
while (getchar() != 'y') /* get response, compare to y */
{
if (getchar() == 'n');
printf("Is it higher or lower?: ");
{
if (getchar() == 'h')
printf("Is it %ld?: ", hg);
else if (getchar() == 'l');
printf("Is it %ld?: ", lg);
}
printf("\nend of loop\n");
}
printf("I knew I could do it!\n");
fflush(stdin);
printf("\nPress Enter to continue.");
getchar();
return 0;
}
-
Your problem is in your while statement. The first issue is that each time you declare getchar() you are getting a different character. What you need to do is make a new character varable, lets say x and then say
Code:
while ((x=getchar()) != 'y')
and then
etc.
Your other problem is that you never actually modify the values for min, max, hg, and lg...so the program will just keep spitting out the same values no matter what. I would suggest that you have a variable for min, max, and guess. Initiate min as zero and max as 100. Find the average of the two and set guess to be that. Display this result and then have the while loop. Inside the while loop, if the user says the guess is too high, set max to equal guess, and then recalculate guess. If the guess is too low, set min to equal guess, and then recalculate guess. Keep doing this until the guess is correct.
Hope that helps
Crazed
-
Thanks a bunch that helped. I think the only thing I am missing are some { } or a ; because the output gets a bit jumbled up.
Code:
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int min = 0;
int max = 100;
int guess;
char x;
guess = (min + max) / 2;
printf("Is your number %d?: ", guess);
while ((x=getchar()) != 'y')
{
if (x== 'n');
printf("Is your number higher or lower?: ");
if (x== 'l')
max = guess;
guess = (min + max) / 2;
printf("Is your number %d?: ", guess);
if (x== 'h');
min = guess;
guess = (min + max) / 2;
printf("Is your number %d?: ", guess);
}
fflush(stdin);
printf("\nPress Enter to continue.");
getchar();
return 0;
}
Can anyone see where I am munking this up?
-
Code:
{
if (x== 'n');
printf("Is your number higher or lower?: ");
if (x== 'l')
max = guess;
guess = (min + max) / 2;
printf("Is your number %d?: ", guess);
if (x== 'h');
min = guess;
guess = (min + max) / 2;
printf("Is your number %d?: ", guess);
}
There was your problem. When you're using if statements, you must either make it inline (code all on one line (for issuing one command)) which I do not advise. You must use curly brackets {} for grouping together the parts of each if statement. This code works:
Code:
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int min = 0;
int max = 100;
int guess;
char x;
guess = (min + max) / 2;
printf("Is your number %d?: ", guess);
while ((x=getchar()) != 'y')
{
if (x== 'n'){
printf("Is your number higher or lower?: ");
}
if (x== 'l'){
max = guess;
guess = (min + max) / 2;
printf("Is your number %d?: ", guess);
}
if (x== 'h'){
min = guess;
guess = (min + max) / 2;
printf("Is your number %d?: ", guess);
}
}
fflush(stdin);
printf("\nPress Enter to continue.");
getchar();
return 0;
}
Have fun, and good luck with C!
-
Excellent that worked perfect thanks for the help folks!
-
> fflush(stdin);
This is bad - see the FAQ