www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/30/23:01:37

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Testing for float or int
Date: Thu, 30 Apr 1998 22:54:48 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 37
Message-ID: <35493978.6AF8@cs.com>
References: <35481B84 DOT 167E AT jcu DOT edu DOT au>
NNTP-Posting-Host: ppp126.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Oon Lin wrote:
> 
> I had been surfing around Delorie's lib C online reference for a
> function that can test whether an input is in float or int.

You could always write one yourself that tests for a decimal point in a
numeric value.  I wrote something like it to handle floating point input
fields in a custom-built TUI.  This basically checks each input
character:  if it's numeric it is accepted; if it is non-numeric it is
rejected, and if it's a period it's only accepted if there has not been
a previous period in the same number.

A very simple function might look like this:

int isfloat( const char *str )
{
    for ( ; *str; str++ )
        if ( !isdigit( *str ) )
            if ( *str == '.' )
                return 1;
            else
                return 0;
    return 0;
}

The only true condition is if a sequence of digits leads up to a period;
otherwise the function returns false.

The above code is tested, btw.  :-)

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Animals can be driven crazy by pla- |
|       aka Fighteer I       | cing too many in too small a pen.    |
|   mailto:fighteer AT cs DOT com   | Homo sapiens is the only animal that |
| http://www.cs.com/fighteer | voluntarily does this to himself."   |
---------------------------------------------------------------------

- Raw text -


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