Thread: Guess my number if and while statements gone haywire.

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    13

    Guess my number if and while statements gone haywire.

    I'm still a noobie trying to teach himself C using C Primer Plus by Prata. This program is supposed to guess my number in x amount of guesses depending on the range. It seems, on the surface, that I can get the 'y' , 'n' & 'l' conditions to work but the 'h' condition hangs up and the cursor just blinks at me. And I'm not too sure if the range is actually updating like I think it is.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    void GetRid(void);
    
    
    int main()
    {
        int num, answer;
        int highRange = 100, lowRange = 1;
        int range = highRange + lowRange - 1;
        int computerGuess = range / 2;
        int maxTries = (log(range)/ log(2));
        int numTries = 1;
        
        printf("pick a number between 1 & 100 and I'll quess it.\n");
        printf("answer 'y' for yes and 'n' for no.\n");
        printf("is your number %d?\n", computerGuess);
        
        while((answer = getchar()) != 'y' && numTries <= maxTries)
        {
            if(answer == 'n')
            {
                GetRid();
                
                printf("Is your number higher 'h' or lower 'l'?\n");
                
                if(getchar() == 'l')
                {
                    GetRid();
                    
                    highRange = computerGuess - 1;
                    computerGuess /= 2.0;
                    printf("is your number %d?\n", computerGuess);
                }
                
                else if(getchar() == 'h')
                {
                    GetRid();
                    
                    lowRange = computerGuess + 1;
                    computerGuess /= 2;
                    printf("is your number %d?\n", computerGuess);
                }
                numTries++;
            }
            else
                printf("\nonly understand 'y' or 'n'.\n");
        }
        
        GetRid();
        
        if(numTries > maxTries)
            printf("too many tries");
        else
            printf("i knew i could do it and in %d tries!\n", numTries);
        
        return 0;
    }
    void GetRid(void) //get's rid of end of line character
    {
        int num;
        while((num = getchar()) != '\n')
            continue;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Lines 30 and 39 need to test the same char.
    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.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    13
    Salem... Thank you. I think I'm a lil in over my head on what I thought I was creating here cuz I'm not understanding that at all.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Every time you call getchar() you get the next character from the input stream. By calling getchar() on the two lines Salem indicates, you are testing different characters in each part of the if/else, but you want to test the same character in both places.
    Code:
    ch = getchar();
    if (ch == 'h') {
    }
    else if (ch == 'l') {
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Aug 2017
    Posts
    13
    Ahhh... Got it! Guess that was a brain fart on my part Just needed the visual, I reckon. Thank you @john.c and @salem!

  6. #6
    Registered User
    Join Date
    Aug 2017
    Posts
    13
    I finally got it... This has now become my new labor of love for the weekend. Here is my finished program... Any suggestions on how I could improve this thing? I feel I might have overcomplicated the thing.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    void GetRid(void);
    
    
    int main()
    {
    	int num, answer;
    	int highRange = 100, lowRange = 1;
    	int range = highRange + lowRange - 1;
    	int computerGuess = range / 2;
    	int maxTries = ceil(log(range)/ log(2));
    	int numTries = 1;
    	
    	printf("pick a number between 1 & 100 and I'll quess it.\n");
    	printf("answer 'y' for yes and 'n' for no.\n");
    	printf("is your number %d?\n", computerGuess);
    	
    	while((answer = getchar()) != 'y' && numTries <= maxTries)
    	{
    		if(answer == 'n')
    		{
    			GetRid();
    		    
    		    printf("Is your number higher 'h' or lower 'l'?\n");
    		    
    		    answer = getchar();
    		    
    		    if(answer == 'l')
    		    {
    		        GetRid();
    		        highRange = computerGuess - 1;
    		        computerGuess = (int)ceil((highRange + lowRange) / 2.0);
    		        printf("is your number %d?\n", computerGuess);
    		    }
    		    
    		    else if(answer == 'h')
    		    {
    		    	GetRid();
    		    	lowRange = computerGuess + 1;
    		    	computerGuess = (int)ceil((highRange + lowRange) / 2.0);
    		    	printf("is your number %d?\n", computerGuess);
    		    }
    		    numTries++;
    		}
    		else
    		    printf("\nonly understand 'y' or 'n'.\n");
    	}
    	
    	GetRid();
    	
    	if(numTries > maxTries)
    	    printf("too many tries");
    	else
    	    printf("i knew i could do it and in %d tries!\n", numTries);
    	
    	return 0;
    }
    void GetRid(void)
    {
    	int num;
    	while((num = getchar()) != '\n')
    	    continue;
    }

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    There is a log2 function (c99).

    The maximum number of guesses needed for 1 to 32 is log2(32) + 1 since a binary tree of depth 5 only holds 31 values.

    Your code can be instantly simplified a little.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    void GetRid(void);
    
    int main()
    {
        int answer;
        int highRange = 100, lowRange = 1;
        int range = highRange + lowRange - 1;
        int computerGuess = range / 2;
        int maxTries = log(range) / log(2) + 1 ;
        int numTries = 1;
         
        printf("pick a number between 1 & 100 and I'll quess it.\n");
        printf("answer 'y' for yes and 'n' for no.\n");
        printf("is your number %d?\n", computerGuess);
         
        while((answer = getchar()) != 'y' && numTries <= maxTries)
        {
            GetRid();
            if(answer == 'n')
            {
                printf("Is your number higher 'h' or lower 'l'?\n");
                 
                answer = getchar();
                GetRid();
                 
                if(answer == 'l')
                    highRange = computerGuess - 1;
                else if(answer == 'h')
                    lowRange = computerGuess + 1;
                computerGuess = (highRange + lowRange) / 2;
                printf("is your number %d?\n", computerGuess);
                numTries++;
            }
            else
                printf("\nonly understand 'y' or 'n'.\n");
        }
    
        if(numTries > maxTries)
            printf("too many tries");
        else
            printf("i knew i could do it and in %d tries!\n", numTries);
    
        return 0;
    }
    
    void GetRid(void)
    {
        int num;
        while((num = getchar()) != '\n')
            continue;
    }
    Last edited by john.c; 03-04-2018 at 02:39 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    Aug 2017
    Posts
    13
    Brilliant! Much cleaner. Sometimes it'z the simple things. Thank you @john.c! Good knowing about the log2() too. Thanx for taking the time to help a noob too, yah?!

  9. #9
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    I don't have the math knowledge to understand the maxTries thing. I did this exercise from chapter 8 last June, and I'm only starting chapter 13 now. Moving slowly enough already so I only do the minimal viable product.
    Code:
    #include <stdio.h>
    int main(void) {
      int guess = 50;
      char hilow;
      int min = 1, max = 100;
    
      printf("Pick an integer from 1 to 100. I will try to guess ");
      printf("it.\nRespond with a y if my guess is right and with");
      printf("\nan l if it is low and with an h if it is high.\n");
      printf("Uh...is your number %d?\n", guess);
      while ((hilow = (char)getchar()) != 'y') { /* get response, compare to y */
        if ('h' == hilow) {
          max = guess - 1;
          guess = min + (max - min) / 2;
        } else if ('l' == hilow) {
          min = guess + 1;
          guess = min + (max - min) / 2;
        }
        if (hilow != '\n')
          printf("Well, then, is it %d?\n", guess);
      }
      printf("I knew I could do it!\n");
    }
    It kind of bothers me that I've repeated myself with
    Code:
    guess = min + (max - min) / 2;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  2. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  3. Guess My Number (Need help)
    By dhardin in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2009, 12:59 PM
  4. can u guess my number?
    By hotsox in forum C Programming
    Replies: 2
    Last Post: 01-07-2006, 01:40 PM
  5. can u guess my number?
    By strider496 in forum C Programming
    Replies: 16
    Last Post: 03-22-2005, 10:19 PM

Tags for this Thread