[comp.lang.c] I want the ultimate robust float input routine

dmocsny@uceng.UC.EDU (daniel mocsny) (02/03/90)

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@uceng.uc.edu

garth_kidd@f813.n680.fido.oz (garth kidd) (02/25/90)

Original to: dmocsny
 > 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.

I had a help() routine that you called if the user asked for 
help. It was used in a lot of my routines, most importantly 
the routine listMgr() that handled scrolling lists of 
information...

help() looked at the global variable (char **)curHelp for 
the information to display. So, for each major area of the 
program, I'd...


/* outside the routine */

char **fooHelp = {
  "Line 1",
  "Line 2",
  "Line 3",
  NULL };

/* and inside */

curHelp=fooHelp;



Worked a treat! The help() routine used listMgr() to display 
the help, so I had to do some workarounds... a static 
variable in help() called helpLevel, and this workaround in 
help():

  if(helpLevel==2)
    return;
  helpLevel++;
  oldHelp=curHelp;
  curHelp=helpOnHelp;

  /* ... code to call listMgr, etc */

  curHelp=oldHelp;
  helpLevel--;
  return;

So, if I asked for help whilst somewhere, helpLevel would 
become 1, curHelp would be helpOnHelp, and listMgr() would 
be invoked. If help was asked for again from within listMgr 
(the user hit ?), helpLevel would become 2, and the help on 
help would be displayed via listMgr(). Any further attempts 
to get help on help on help would be ignored until the user 
dropped back...

Hope this help()ed. :-)


gk

--- FD 1.99b
 * Origin: JC's UnderWater BBS - You want tech? You got it! (3:680/813)