home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / libcs / getdouble.c < prev    next >
C/C++ Source or Header  |  1990-12-11  |  3KB  |  107 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. /*  getdouble --  prompt user for double
  29.  *
  30.  *  Usage:  f = getdouble (prompt,min,max,defalt)
  31.  *    double f,min,max,defalt;
  32.  *    char *prompt;
  33.  *
  34.  *  Getdouble 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 double (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 getdouble.
  43.  *  On end-of-file or read error in the standard input, the default
  44.  *  is returned.
  45.  *
  46.  *  HISTORY
  47.  * $Log:    getdouble.c,v $
  48.  * Revision 1.2  90/12/11  17:54:05  mja
  49.  *     Add copyright/disclaimer for distribution.
  50.  * 
  51.  * 28-Apr-85  Steven Shafer (sas) at Carnegie-Mellon University
  52.  *    Modified for 4.2 BSD.  Now uses stderr for output.
  53.  *
  54.  * 23-Oct-82  Steven Shafer (sas) at Carnegie-Mellon University
  55.  *    Added code to return default on EOF or error in standard input.
  56.  *
  57.  * 20-Nov-79  Steven Shafer (sas) at Carnegie-Mellon University
  58.  *    Created for VAX.
  59.  *
  60.  */
  61.  
  62. #include <stdio.h>
  63. #include <ctype.h>
  64. #include <math.h>
  65.  
  66. double getdouble (prompt,min,max,defalt)
  67.     double min,max,defalt;
  68.     char *prompt;
  69. {
  70.     char input [200];
  71.     register char *p;
  72.     register int err;
  73.     double i;
  74.  
  75.     fflush (stdout);
  76.     do {
  77.  
  78.         fprintf (stderr,"%s  (%g to %g)  [%g]  ",prompt,min,max,defalt);
  79.         fflush (stderr);
  80.  
  81.         err = 0;
  82.         if (gets(input) == NULL) {
  83.             i = defalt;
  84.             err = (i < min || max < i);
  85.         }
  86.         else {
  87.             for (p=input; *p &&
  88.              (isdigit(*p) || *p=='-' || *p=='.' || *p=='+'
  89.               || *p=='e' || *p=='E');
  90.              p++);
  91.     
  92.             if (*p) {        /* non-numeric */
  93.                 err = 1;
  94.             } else {
  95.                 if (*input)    i = atof (input);
  96.                 else        i = defalt;
  97.                 err = (i < min || max < i);
  98.             }
  99.         }
  100.  
  101.         if (err) fprintf (stderr,"Must be a number between %g and %g\n",
  102.             min,max);
  103.     } while (err);
  104.  
  105.     return (i);
  106. }
  107.