cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/14/19:35:55

Message-Id: <199709142333.SAA08714@fly.HiWAAY.net>
Reply-To: <kwall AT utw DOT com>
From: "Kurt Wall" <kwall AT utw DOT com>
To: <anfamily AT sprintmail DOT com>
Cc: <djgpp AT delorie DOT com>
Subject: Re: checking types
Date: Sun, 14 Sep 1997 18:32:21 -0500
MIME-Version: 1.0

I presume you are using C.  This is only one of many problems with scanf. 
The "standard" (that is, usual) approach is to read an entire line using
fgets() (see your docs), then scan through the string using isdigit() and
such (not ANSI-standard, I think).  Another approach is to use getch() to
read character input one character at a time, then use the ANSI conversion
functions to turn the input into something useful.

For example:
#include <stdio.h>
#define LIMIT 100
int i, c;
char num[limit];
main()
{
    i = 0;
    /* read characters from stdin until 
    * 1) we get an EOF
    * 2) a newline
    * 3) approach the buffer limit
    */
    while((c=getchar()) != EOF && c != '\n' && i < LIMIT - 1) {
        /* if the input is numeric, store it */
        if(c >= '0' && c <= '9') {
            num[i] = c;
            i++;
    }
    if(c == '\n') num[i] = '\0';
    i = atoi(num);
    if(num != 0) printf("%d\n", num);
    return 0;
}

			

----------
> From: anfamily AT sprintmail DOT com
> To: djgpp AT delorie DOT com
> Subject: checking types
> Date: Saturday, September 13, 1997 5:00 PM
> 
> 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?
> -- 
> (~_Ionicis_~)
> http://www.geocities.com/SiliconValley/Bay/2306/ <--- Go here!

- Raw text -


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