Having Trouble With isdigit Exercise

This is a discussion on Having Trouble With isdigit Exercise within the C Programming forums, part of the General Programming Boards category; I have tried for sometime now with this exercise Build a number guessing game that uses input validation (isdigit() function) ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    38

    Having Trouble With isdigit Exercise

    I have tried for sometime now with this exercise

    Build a number guessing game that uses input validation

    (isdigit() function) to verify that the user has entered a digit

    and not a non-digit (letter). Store a random number between

    1 and 10 into a variable each time the program is run. Prompt

    the user to guess a number between 1 and 10 and alert the user

    if he was correct or not.


    I wrote the following code

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    
    {
    	int  iRandomNum;
    	int  iResponse;
    
    	iRandomNum = (rand()%10)+1;
    
    	printf("\nGuess a number between 1 and 10: ");
    	scanf("%d", &iResponse);
    	
    	if (isdigit(iResponse))
    		;
    	else
    		{
    		printf("You didn't enter a number\n\n");
    		return 0;
    	}
    			
    	if (iResponse == iRandomNum) {
    		printf("\nYou guessed right\n"); 	
    		}	
    		else {
    		printf("\nSorry, you guessed wrong\n");
    		printf("The correct guess was %d\n", iRandomNum);
    	} 
    }
    I can't get both if statements to work after one another. I also get a warning ,

    ".c(10): warning #2027: Missing prototype for 'rand'."

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You need to seed rand() with srand() before using it, rand() is declared in stdlib.h.

    scanf uses a %d format, use %c and declare iResponse as a char.

    You can also check directly if isdigit is NOT a digit.
    Code:
    	if (!isdigit(iResponse))
            {
    		printf("You didn't enter a number\n\n");
    		return 0;
    	}
    Last edited by Subsonics; 02-22-2011 at 07:03 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    38
    It's still not working but I no longer receive any warnings.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    int main()
    
    {
    	int  iRandomNum;
    	int  iResponse;
    	srand(1-10);
    	
    	iRandomNum = (rand()%10)+1;
    
    	printf("\nGuess a number between 1 and 10: ");
    	scanf("%d", &iResponse);
    	
    	if (isdigit(iResponse))
    		;
    	else
    		{
    		printf("You didn't enter a number\n\n");
    		return 0;
    	}
    			
    	if (iResponse == iRandomNum) {
    		printf("\nYou guessed right\n"); 	
    		}	
    		else {
    		printf("\nSorry, you guessed wrong\n");
    		printf("The correct guess was %d\n", iRandomNum);
    	} 
    }
    I will enter a number and receive the message "you didnt enter a number." I don't see how this can be since isdigit is supposed to verify that iResponse is a digit even though its an integer to begin with.

    If I turn iResponse to a char then the 2nd if will execute but fail to produce desired results since iResponse would now be a char and iRandomNum an int.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by BrandNew View Post
    If I turn iResponse to a char then the 2nd if will execute but fail to produce desired results since iResponse would now be a char and iRandomNum an int.
    Sure, but if iResponse is an int how can it be anything but a digit?

    What you encounter is an ascii representation of 0-9. You can do the comparison by subtracting '0' from iResponse, like this. if( (iResponse - '0') == iRandom )

    Look up a ascii table, to see the reason behind this.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    	if (isdigit(iResponse))
    		;  <---- error
    	else
    		{
    		printf("You didn't enter a number\n\n");
    		return 0;
    See the semicolon I marked as an error? What you have there is the functional equivalent of:
    Code:
    if (isdigit(iResponse));
    which does nothing.

    If you want to check if something is not a digit use the logical NOT operator...
    Code:
    if ( ! isdigit(iResponse))
      { printf("You didn't enter a number\n");
         return 0; }
    Then it will work.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's not an actual error, it's functionally equivalent, but a some what unusual way of doing it.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    38
    Thanks for the help everyone it finally worked. The final version is :
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    int main()
    
    {
    	int  iRandomNum;
    	char  iResponse;
    	srand(1-10);
    	
    	iRandomNum = (rand()%10)+1;
    
    	printf("\nGuess a number between 1 and 10: ");
    	scanf("%c", &iResponse);
    
    	if (!isdigit(iResponse)) {
    		printf("You didn't enter a number\n\n");
    		return 0;
    	}
    			
    	if ( (iResponse - '0')== iRandomNum) {
    		printf("\nYou guessed right\n"); 	
    		}	
    		else {
    		printf("\nSorry, you guessed wrong\n");
    		printf("The correct guess was %d\n", iRandomNum);
    	} 
    }
    For some reason
    Code:
    if (!isdigit(iResponse))
    would always be deemed true if iResponse was declared as an int which leads me to believe isdigit shouldn't be used with int variable type

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, for a single character of input your range is 0 to 9 ... not 1 to 10. Entering 10 is two digits and overflows a single character.

    This means several changes... you need to lose the +1 on your random number generator so that it can produce 0 to 9, you need to change your entry prompt...

    Also as a matter of performance you should seed the random number generator before using it, otherwise it will always generate the same sequence... srand(1-10) being a fixed value will also generate the same sequence every time.
    srand(time(0)); is usually adequate as the value of time() changes every second.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-02-2010, 01:49 AM
  2. Replies: 3
    Last Post: 06-02-2009, 06:13 PM
  3. Line of data input method
    By larry_2k4 in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 11:34 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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