I want the ultimate robust float input routine

daniel mocsny dmocsny at uceng.UC.EDU
Sat Feb 3 16:53:49 AEST 1990


Hello.

I am writing a C program that must interactively prompt a user for a
floating-point number. Right now I'm using a fairly naive extension of
one of E. Gray's get_num() routines from his pcomm program. My
get_float() is smart enough to restrict the user to typing in only the
characters '+', '-', '.', [0-9], 'e', and 'E'. It returns a pointer to
char, because the calling program needs to detect the user typing only
a <Return> (accept a default), or <Escape> (abort to previous menu).
So after screening for those cases, the calling routine hands the
returned string to atof().

And lo! Certain subtly garbaged input strings make my program barf its
core at the call to atof() or shortly thereafter. Things like
unreasonably large exponents, e.g., 1.0e+300. Apparently I need to
juice up my get_float() function so that it traps every
possible invalid input. This seems like a straightforward job, but
I suppose I'd be about the 1.0e+300'th person to re-invent this
particular sledge.

Therefore, I appeal to the collective intellectual might of this
forum.  Who has the ultimate interactive float input routine, that
they might care to share? Or pointers to publicly available sources?

While I'm shamelessly begging, how about a nicely implemented
method to provide context-sensitive on-line help? My current
method is unspeakably hideous, but easy to implement. Just
declare a bunch of template help functions (help1(), help1a(),
...), and let each one open and shut down its own curses window.
This is easy to program, given an editor with block copy, but
it pumps all the help screens through the compiler and bloats
the executable. An obviously better solution would be to put
all the help screens into an indexed file that a single function
would seek into in response to a calling argument. Once again,
every other C programmer must have already done this. Any
suggestions, or suggestions on where to look?

Post or e-mail, as you will. If anyone is interested, I'll post
a summary.

Dan Mocsny
dmocsny at uceng.uc.edu



More information about the Comp.lang.c mailing list