Thread: isdigit() Function Again -_-

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    isdigit() Function Again -_-

    Code:
    #include <stdio.h>
    
    #include <ctype.h>
    
    #include <time.h>
    
    #include <stdlib.h>
    
    
    
    int main()
    
    {
    
    	int iRandNum = 0;
    
    	int iUserGuess = 0;
    
    	srand(time(NULL));
    
    	iRandNum = (rand() % 10) + 1;
    
    	printf("Guess the number from 1 to 10\n");
    
    	scanf("%i", &iUserGuess);
    
    	if (isdigit(iUserGuess) == 0) 
    
    		{ 
    
    			printf("Invalid Entry, Not a number!\n"); 
    
    		}
    
    	else if (isdigit(iUserGuess) && iUserGuess == iRandNum)
    
    		{
    
    			printf("You guessed it!\n");
    
    		}
    
    	else if (isdigit(iUserGuess) && iUserGuess != iRandNum)
    
    		{
    
    		printf("Wrong Guess! Number was: %i\n", iRandNum);
    
    		}
    
    	return 0;
    
    }
    Could someone please explain the isdigit function a bit better, because that's the problem with it.. But I'm not sure how exactly I'm supposed to be using it and why this isn't working, Thanks.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'll explain it, but you don't need it for this particular program. scanf will take the user's input and do any conversions necessary to represent the data as long as you give it a pointer to (or the address of a variable which is) the correct data type.

    You're familiar with all the numeral characters, right? '0', '1', '2', '3' and so on.

    The isdigit function recieves a character that might be a digit, and if it is, it returns nonzero.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Code:
    result=isdigit(iUserGuess);
    in this iuserguess is the integer guessed by the user ...if the iuserguess is an integer means result will be nonzero elese if the iuserguess is not an integer it will be zero...
    Last edited by sreeramu; 05-23-2008 at 01:35 AM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by sreeramu View Post
    Code:
    isdigit(iUserGuess)
    in this iuserguess is the integer guessed by the user ...if the iuserguess is an integer means it will return nonzero elese if the iuserguess is not an integer it will return zero...
    This makes no sense at all

    PS. This thread is duplicate of an other...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Big & Little Wong Tin-Bar Jackie Chan's Avatar
    Join Date
    May 2008
    Location
    Hong Kong
    Posts
    23
    This small change in your code should help you:
    Code:
    if (scanf("&#37;i", &iUserGuess) == 0)
    /*if (isdigit(iUserGuess) == 0) */
    { 
    	printf("Invalid Entry, Not a number!\n"); 
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Thre things (at least) are wrong here:
    1. If you enter something other than a number to scanf, it will not end up in iuserguess.

    2. iuserguess is ALWAYS an integer value - it may not have a defined value if you haven't set it to anything sensible, but it will always be an integer.

    3. isdigit() checks if the input is in the range '0' .. '9' (and potentially, if you set your locale right, their equivalents in script languages such as Japanese, Thai or Chinese) - not "is the integer passed in really an integer" - because an integer in C is ALWAYS an integer. If we assume you use ASCII-based characters, isdigit() would be true for the range 48..57, and untrue for anything else.

    If you want to check if the user types in a valid number (note that NUMBER is different from a valid DIGIT), you will need to approach it in a different way. There are two essential choices:
    1. Read the supposed number as a string, then attempt to translate to a number and tell the user "you entered something wrong" if it didn't fully work to form a number from the string.
    2. Check the result coming back from scanf() - scanf() will return the number of correctly translated "items" that it has done. In your case, there's only two choices [1], One or Zero. Zero means that nothing was correctly read in, and one means that something that scanf() accepts as a number was entered. Note that scanf() stops reading as soon as it finds something that isn't valid, leaving whatever was invalid in the input buffer. So if someone types in "abc" instead of a number, you would be left with "abc" in the input buffer, and next time you call scanf(), it will still find "abc" and not accept it. So you need to clear the buffer. Look in the FAQ for "How do I clear the input buffer".

    Both of these approaches have their benefits and drawbacks.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM