Number guessing.

This is a discussion on Number guessing. within the C Programming forums, part of the General Programming Boards category; Hi Folks I have the following number guessing code. Using averages I want to narrow down the numbers but can't ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    3

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

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    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
    Code:
    if (x== 'n');
    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

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    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?

  4. #4
    Registered User
    Join Date
    Apr 2006
    Location
    Plymouth, U.K.
    Posts
    13
    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!

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    3
    Excellent that worked perfect thanks for the help folks!

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > fflush(stdin);
    This is bad - see the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 06:19 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. My number guessing game
    By The Gweech in forum C++ Programming
    Replies: 7
    Last Post: 06-22-2002, 09:03 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21