Mail Archives: djgpp/1997/09/15/07:42:13
On Sat, 13 Sep 1997 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.
The simplest yet reliable way I know to deal with input of integer
values is to read it as a string (e.g., with `fgets'), then pass it to
`strtol'. `strtol' returns a pointer to the first character that is
NOT part of a number. So, if the user types a number, that pointer
will point right to the terminating null; if it points before that,
yell at the user.
- Raw text -