0

Heya. I'm having a problem with my guessing game. It won't stop guessing even after getting the right answer, and it also prints both questions no matter what the user input is. The guessing game is one where the computer guesses the user's number...any help?

#include <stdio.h>
#define max 200

main()
{
    int x, guesses, low, high, mid;
    char response;
    size=max;
    x=100;
    low=1;
    high=max;
    mid=(low+high)/2;
    printf ("Pick an integer from 1 to 200 and I will guess it.");
    printf ("Respond with y is I'm right, n if I'm wrong.\n");
    printf ("I will attempt to guess nine times.");
    while (low<=high) 
    {
          printf ("Is your number %d?\n", mid);
          response=getchar();
          if (response=='n')
          {
               printf ("Is the number less than %d?\n", mid);
               response=getchar();
               if (response=='n')
               {
                     low=mid;
                     mid=(low+high)/2;
               }
               else
               {
                     high=mid;
                     mid=(low+high)/2;
               }
          }
          else if (response=='y') 
          printf ("I got it!\n");
          }
          getch();
}
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Adak
0

click on the "Snippets" tab, and study the Binary Search snippet.

Your logic is *way* off the mark.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.