Binary search use in simple guessing game 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 &lt;stdio.h&gt;
#include &lt;ctype.h&gt;

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", &amp;guess);
printf("Please respond with L, H, or Y.\n");
scanf("%d", &amp;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;
}
I'm thoroughly puzzled by your use of the response variable.

The condition for the while-loop is response != 'Y'.  The first time through, response is an uninitialized variable, containing any imaginable int value.  So, the behaviour of your program is undefined.

response is an int.  Why are you comparing it to 'Y'?  That makes 89 a special number.

If we make it inside the loop, you ask the user to enter a character 'L', 'H', or 'Y', but actually read an integer.  If you actually enter one of those three characters, scanf() will fail to read an integer; your program will consider the input to be invalid, and it will loop forever, since scanf() will immediately fail again, etc.  There are only two ways to terminate your program: CtrlC or by typing 89Enter at the prompt.

Then you call toupper() on the integer; the toupper() function only makes sense for translating a letter to its uppercase counterpart.  (The toupper() function takes an int instead of a char only because it is meant to support EOF, or -1, as a value.)

Then, in the switch block, you have magic numbers 72, 76, and 89.  Magic numbers should not exist in your code.



Your first printf() call is also wrong.  It should be

printf("Okay, let's start. Is it %d?\n", guess);


since &amp;guess would print the address of the guess variable instead of its contents.  Your compiler should have warned you about that mistake.  You should have heeded the warning, as well as tested your code.