I'm making a simple guessing game in C. Basically what I need is some help double-checking my work, more specifically a verification that the my binary search in my case statements look okay. The goal is to have an initial guess of 50, then work off that. If "L" is selected, the search goes from 51-100, if "H", it would be 1-49, blah-blah - the usual stuff scaling down to the correct guess.
Is this correct?
I'm also getting a C99 function implementation error using the actual letters as case labels -- just to explain why they're the ASCII equivalents.
#include <stdio.h>
#include <ctype.h>
int main(int argc, const char * argv[])
{
int low;
int high;
int guess;
int response;
printf("Pick an integer from 1 to 100 and I will try to guess it.\n");
printf("If I guess too low, respond with a L.\n");
printf("If I guess too high, respond with a H.\n");
printf("If I guess correctly, respond with a Y.\n");
high = 100;
low = 1;
while(response != 'Y')
{
guess = (high + low) / 2;
printf("Okay, let's start. Is it %d?\n", &guess);
printf("Please respond with L, H, or Y.\n");
scanf("%d", &response);
response = toupper(response);
switch(response)
{
case 72: high = guess - 1;
break;
case 76: low = guess + 1;
break;
case 89: printf("Yes! I knew I could do it.\n");
break;
default: printf("I didn't get that; you gave an invalid response.\n");
printf("Please try again.\n");
}
}
return 0;
}