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;
}