Mail Archives: djgpp/1997/09/14/14:58:49
anfamily AT sprintmail DOT com wrote:
>
> How would you check a variable to make sure it's a certain data type you
> want it to be? I have a small prog, that needs this. It prompts you
> for an integer, but you can enter other types. If you enter other
> types, if really screws up the prog. I'm using a scanf to get that
> value for a variable, but after that, I want to check to make sure the
> value in that variable is a integer. If it's not, it would prompt the
> user again. How would I do that?
The value read by scanf as %d will end up being an integer --
it just may not be an integer that you want. You can always
read it into a temporary itemp, then check that itemp is within
the desired limits.
Alternatively, you can read in a string, then either extract
the value with sscanf, or check for warning signs in the tokens--
like character ascii values that are not in >='0' and <='9'.
I used scansets before to restrict the characters read in, but
it has been so long, I don't know if these ever made it into
the ansi standard.
- Raw text -