home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / libcs / getfloat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-11  |  3.4 KB  |  110 lines

  1. /*
  2.  * Copyright (c) 1990 Carnegie Mellon University
  3.  * All Rights Reserved.
  4.  * 
  5.  * Permission to use, copy, modify and distribute this software and its
  6.  * documentation is hereby granted, provided that both the copyright
  7.  * notice and this permission notice appear in all copies of the
  8.  * software, derivative works or modified versions, and any portions
  9.  * thereof, and that both notices appear in supporting documentation.
  10.  *
  11.  * THE SOFTWARE IS PROVIDED "AS IS" AND CARNEGIE MELLON UNIVERSITY
  12.  * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.  IN NO EVENT
  14.  * SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY SPECIAL, DIRECT,
  15.  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  17.  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  *
  20.  * Users of this software agree to return to Carnegie Mellon any
  21.  * improvements or extensions that they make and grant Carnegie the
  22.  * rights to redistribute these changes.
  23.  *
  24.  * Export of this software is permitted only after complying with the
  25.  * regulations of the U.S. Deptartment of Commerce relating to the
  26.  * Export of Technical Data.
  27.  */
  28. /*  getfloat --  prompt user for float
  29.  *
  30.  *  Usage:  f = getfloat (prompt,min,max,defalt)
  31.  *    float f,min,max,defalt;
  32.  *    char *prompt;
  33.  *
  34.  *  Getfloat prints the message:  prompt  (min to max)  [defalt]
  35.  *  and accepts a line of input from the user.  If the input
  36.  *  is not null or numeric, an error message is printed; otherwise,
  37.  *  the value is converted to a float (or the value "defalt" is
  38.  *  substituted if the input is null).  Then, the value is
  39.  *  checked to ensure that is lies within the range "min" to "max".
  40.  *  If it does not, an error message is printed.  As long as
  41.  *  errors occur, the cycle is repeated; when a legal value is
  42.  *  entered, this value is returned by getfloat.
  43.  *  The default is returned on EOF or error in the standard input.
  44.  *
  45.  *  HISTORY
  46.  * $Log:    getfloat.c,v $
  47.  * Revision 1.2  90/12/11  17:54:15  mja
  48.  *     Add copyright/disclaimer for distribution.
  49.  * 
  50.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  51.  *    Modified for 4.2 BSD.  Now uses stderr for output.
  52.  *
  53.  *  5-Nov-84  Glenn Marcy (gm0w) at Carnegie-Mellon University
  54.  *    Changed i to a double for extra precision in comparisons.
  55.  *
  56.  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
  57.  *    Added code to return default on error or EOF on standard input.
  58.  *
  59.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  60.  *    Created for VAX.
  61.  *
  62.  */
  63.  
  64. #include <stdio.h>
  65. #include <ctype.h>
  66. #include <math.h>
  67.  
  68. float getfloat (prompt,min,max,defalt)
  69. float min,max,defalt;
  70. char *prompt;
  71. {
  72.     char input [200];
  73.     register char *p;
  74.     register int err;
  75.     double i;
  76.  
  77.     fflush (stdout);
  78.     do {
  79.  
  80.         fprintf (stderr,"%s  (%g to %g)  [%g]  ",prompt,min,max,defalt);
  81.         fflush (stderr);
  82.  
  83.         err = 0;
  84.         if (gets(input) == NULL) {
  85.             i = defalt;
  86.             err = (i < min || max < i);
  87.         }
  88.         else {
  89.             for (p=input; *p &&
  90.              (isdigit(*p) || *p=='-' || *p=='.' || *p=='+'
  91.               || *p=='e' || *p=='E');
  92.              p++);
  93.             if (*p) {        /* non-numeric */
  94.                 err = 1;
  95.             } 
  96.             else {
  97.                 if (*input)    i = atof (input);
  98.                 else        i = defalt;
  99.                 err = (i < min || max < i);
  100.             }
  101.         }
  102.  
  103.         if (err) fprintf (stderr,"Must be a number between %g and %g\n",
  104.         min,max);
  105.     } 
  106.     while (err);
  107.  
  108.     return (i);
  109. }
  110.