Guessing program Hm. Help

This is a discussion on Guessing program Hm. Help within the C Programming forums, part of the General Programming Boards category; The program is suppose to guess the value a user is thinking, the program askes if the number it guessed ...

  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
    21,448
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 10: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

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