www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/14/14:58:49

From: "H.W. Stockman" <hwstock AT swcp DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: checking types
Date: Sat, 13 Sep 1997 16:14:31 -0600
Organization: Lentil Sorbet, Inc.
Lines: 21
Message-ID: <341B1047.3EA9@swcp.com>
References: <341B0CE1 DOT 29B0 AT sprintmail DOT com>
Reply-To: hwstock AT swcp DOT com
NNTP-Posting-Host: ppp23.swcp.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019