Thread: Guessing program Hm. Help

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Guessing program Hm. Help

    The program is suppose to guess the value a user is thinking, the program askes if the number it guessed is higher or lower than the users value. Once the program find the correct number, the program is suppose to display " i knew i could to it" and once the user hits enter, it terminates. I can get the program to display the text. But, after i hit enter it backs in to the loop instead of terminating.

    Can any one help? Thanks,

    Code:
    #include <stdio.h>
    
    
    int main(void)
    
    {
    
    	int hi, lo, guess;
    	char answer;
    
    	hi = 100;
    	lo = 0;
    	guess = (hi - lo) / 2;
    
    	printf( "Pick a number and I will try to guess your number\n\n");
    	
    do
    {
    	printf ("%d:", guess);
    	printf (" is this number lower(l), higher(h), or correct(c)");
    	
    	
    	switch (getchar())
    	{
    	case 'h':
    		hi = guess;
    		guess = lo + (hi - lo)/2;
    		break;
    	case 'l':
    		lo = guess;
    		guess = lo + (hi - lo)/2;
    		break;
    	case 'c':
    		printf(" I knew I could do it");
    		getchar();
    		
    	}
    }
    while ( getchar()!= 'c');
    
    return 0;
    	
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's because you have a getchar() in the end of your dowhile. If you hit c and return at that point, it will exit. You probably, however, would prefer to store the result of the original getchar() answer to the enter l, h or c.

    Also, are you sure this is right:
    guess = lo + (hi - lo)/2;

    I would have thought that the quess would be: guess = (hi + lo) / 2 always.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    And this is C# not C topic group

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    And this is C# not C topic group
    That's right, so this thread has been moved to the C programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. program won't run standalone
    By richard in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2002, 06:21 PM