# Thread: isdigit() Function Again -_-

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

4. Originally Posted by sreeramu
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...

Code:
```if (scanf("&#37;i", &iUserGuess) == 0)
/*if (isdigit(iUserGuess) == 0) */
{
printf("Invalid Entry, Not a number!\n");
}```

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