Stupid problem probably has a stupid answer...

This is a discussion on Stupid problem probably has a stupid answer... within the C Programming forums, part of the General Programming Boards category; A friend sent me a simpler form of the following code (didn't have the high/low check, while loop/max number of ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Stupid problem probably has a stupid answer...

    A friend sent me a simpler form of the following code (didn't have the high/low check, while loop/max number of guesses) and I got bored and threw this together real quick. Problem is it's not working. I'm sure I did something monumentally stupid, anyone see my screwup? (Yes, I know it doesn't currently pick a random number).

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    main ()
    {
    	int iRandom = (rand() % 10) + 1;
    	char cGuess = '\0';
    	int iGuess;
    	int count = 0;
    
    	while (count < 4)
    	{
    		printf("Guess a number 1 through 10: ");
    		scanf("%c", &cGuess);
    		count++;
    
    		if (!isdigit(cGuess))
    			printf("Invalid entry!\n");
    		else
    		{
    			iGuess = cGuess - '0';
    			if (iGuess > iRandom)
    			{
    				printf("You guessed too high.\n");
    			}
    			else if (iGuess < iRandom)
    			{
    				printf("You guessed too low.\n");
    			}
    			else
    			{
    				printf("You got it!\n");
    				break;
    			}
    		}
    	}
    	if (count < 4)
    		return;
    	else
    		printf("You lost! The answer was %d.\n", iRandom);
    	
    }
    And the output...

    Code:
    Guess a number 1 through 10: 2
    You guessed too low.
    Guess a number 1 through 10: Invalid entry!
    Guess a number 1 through 10: 3
    You guessed too low.
    You lost! The answer was 4.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,657
    scanf leaves the newline character in the stdin buffer. Look at the FAQ.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid problem
    By Adrian20XX in forum C Programming
    Replies: 6
    Last Post: 08-23-2008, 09:16 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  3. an answer raises another problem...
    By Dorky King in forum C Programming
    Replies: 2
    Last Post: 06-12-2007, 04:11 PM
  4. Random Numbers...Problem With FAQ Answer
    By sitestem in forum C++ Programming
    Replies: 12
    Last Post: 04-14-2004, 10:22 AM
  5. code help :)
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-28-2002, 01:12 PM

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