Like Tree1Likes
  • 1 Post By AndiPersti

Guessig game

This is a discussion on Guessig game within the C Programming forums, part of the General Programming Boards category; ok so the computers supposed to guess my number however when i go to run it it asks what my ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    23

    Guessig game

    ok so the computers supposed to guess my number however when i go to run it it asks what my repsonse to its guess is and shuts down. Anyone that can help? I would much appreciate it

    Code:
    #include <stdio.h>
    
    int 
    main(void)
    	{
    			/* variables */
    			
    			int min, max, guess;
    			char response;
    		
    			min = 1;
    			max = 100;
    			guess = (max + min / 2);
    			
    			/* Asks the user to pick a number*/
    			
    			printf("Think of a number in the range of 1-100\n");
    			
    			/* Applys first guess */
    			
    			printf("My guess is %d, whats your response? (>,<, or =)\n", guess);
    			scanf("%c", response);
    		
    		while (response != '=')
    			{
    		
    			printf("My guess is %d, whats your response? (>,<, or =)", guess);
    			scanf("%c", response);
    				
    				if (response == '>')
    					{
    						min = ( max + min / 2);
    					}
    				else
    				{
    					if (response == '<')
    					{
    						max = ( max + min / 2);
    					}
    					else
    					{
    						printf("I Win");
    					}
    				}
    			}
    		system("pause");
    		return (0);
    	}

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    After you type your response of <, > or =, you press enter. That enter key is actually a key, so you get a new line ('\n') in your input buffer, which scanf reads when you use "%c". If you put a space before the %c, it will skip any leading white space, including the new line. Also, you need to provide the address of the response variable, so scanf knows where to store the result. If your compiler doesn't complain about that, then turn up the warnings or get a better compiler. Your scanf calls should look like this:
    Code:
    scanf(" %c", &response);
    And just an FYI, system("pause"); will only work on Windows, so you run the risk of alienating people here that use Linux, who could otherwise help you with your problems. There are other ways to keep your console window from disappearing. Read this link: FAQ > Stop my Windows Console from disappearing everytime I run my program? - Cprogramming.com.

    EDIT: Also, you could (IMO) improve the structure of your program a bit by using a do-while loop (so you only have to prompt the user in one place), and using an if-else if structure for comparing the response:
    Code:
    do {
        print "my guess is ... what's your response..."
        read response
        if (response == '<')
            ...
        else if (response == '>')
            ....
        else if (response == '=')
            print "win"
        else
            print "invalid input"
    } while (response != '=');
    Last edited by anduril462; 10-04-2012 at 12:36 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I think it's time for a new FAQ entry. :-)

    Bye, Andreas
    anduril462 likes this.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,469
    Quote Originally Posted by AndiPersti View Post
    I think it's time for a new FAQ entry. :-)

    Bye, Andreas
    I think you just volunteered to write it! I look forward to linking newbies to it.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Frankly, I've thought about it for some days now.
    Probably will have some time tomorrow to write it down :-)

    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have some logic problems.

    If the response is > the guess, you want the min to be assigned the value of guess + 1. If the response is < the guess, you want the max to be assigned to guess - 1.

    Outside the if(response) statement, you have the line of code, guess = (min+max)/2.

    Make sense?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-18-2010, 09:42 PM
  2. Should I use a game engine in making a RPG game?
    By m3rk in forum Game Programming
    Replies: 6
    Last Post: 01-26-2009, 03:58 AM
  3. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 09:53 AM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 06:58 PM
  5. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM

Tags for this Thread


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