home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / pascal / passrc / input.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-10-15  |  4.8 KB  |  148 lines

  1. { -----------------------------------------------------------------------------
  2.  
  3.                                  NOTICE:
  4.  
  5.       THESE MATERIALS are UNSUPPORTED by OSS!  If you do not understand how to
  6.       use them do not contact OSS for help!  We will not teach you how to
  7.       program in Pascal.  If you find an error in these materials, feel free
  8.       to SEND US A LETTER explaining the error, and how to fix it.
  9.  
  10.       THE BOTTOM LINE:
  11.  
  12.          Use it, enjoy it, but you are on your own when using these materials!
  13.  
  14.  
  15.                                DISCLAIMER:
  16.  
  17.       OSS makes no representations or warranties with respect to the contents
  18.       hereof and specifically disclaim all warranties of merchantability or
  19.       fitness for any particular purpose.   This document is subject to change
  20.       without notice.
  21.  
  22.       OSS provides these materials for use with Personal Pascal.  Use them in
  23.       any way you wish.
  24.  
  25.    -------------------------------------------------------------------------- }
  26.  
  27.  
  28. {************************************************************************
  29.  
  30.    INPUT.PAS - function takes a high, low, and default number
  31.                and a prompt and returns when satisfied
  32.                10-14-86 MJC
  33.  
  34.    INPUT( prompt, low, high, def )
  35.  
  36.    Example:
  37.  
  38.         Int_var := Input( 'Enter number from 1 to 100 ', 1, 100, 0 );
  39.  
  40.    would prompt the user for a number, wait for a response,
  41.    check for proper values, and then return.  If a proper value (between
  42.    the Low and High parameter) is not entered, the routine will ask for
  43.    a new response.
  44.  
  45.    Include in your program (*$I input.pas *)
  46.  
  47. ************************************************************************}
  48.  
  49.  
  50.   { val - Convert the number contained in the string 's' to an integer, and
  51.       return that integer as the function result.  We are assuming the caller
  52.       has ensured the string is a valid number, so we we're just going to
  53.       convert characters into the number until a non-digit is encountered, or
  54.       the end of the string is reached.  A zero is returned as the function
  55.       value if the string that was passed has zero length. }
  56.  
  57.   FUNCTION val( s: STRING ): integer;
  58.  
  59.     VAR
  60.       { Flag to indicate the number has a leading minus sign }
  61.       minus: boolean;
  62.       i, n : Integer;
  63.  
  64.     BEGIN
  65.       { Start with the first character of the string, but first skip leading
  66.         blanks. }
  67.       i := 1;
  68.       WHILE (i < length(s)) AND (s[i] = ' ') DO
  69.         i := i + 1;
  70.       { If there are characters still in the string, convert it to a number }
  71.       n := 0;
  72.       IF length(s) >= i THEN
  73.         BEGIN
  74.           { If first char is '-', we have to negate the number after we finish
  75.             converting the digits. }
  76.           IF s[i] <> '-' THEN
  77.             minus := false
  78.           ELSE
  79.             BEGIN
  80.               minus := true;
  81.               i := i + 1;
  82.             END;
  83.           { While there are more digits in the string, convert characters. }
  84.           WHILE (i <= length(s)) AND (s[i] IN ['0'..'9']) DO
  85.             BEGIN
  86.               n := (n * 10) + ord(s[i]) - ord('0');
  87.               i := i + 1;
  88.             END;
  89.           { Negate the final result, if necessary. }
  90.           IF minus THEN
  91.             n := -n;
  92.         END;
  93.       { Return the converted number as the result of this function. }
  94.       val := n;
  95.     END;
  96.  
  97.  
  98. {**************************************************************************
  99.  
  100.         The famous Input routine...
  101.  
  102. ************************************************************************}
  103.  
  104. FUNCTION Input( Prompt : STRING; Low, High, Def : INTEGER ) : INTEGER;
  105.  
  106.  
  107. VAR
  108.  
  109.    Tempval : INTEGER;   { hold temp value... }
  110.    Tempstr : STRING;    { get input }
  111.    Done : BOOLEAN;      { Are we done... }
  112.  
  113.    BEGIN
  114.  
  115.       REPEAT
  116.          BEGIN
  117.             Write( Prompt );  { send prompt string }
  118.             Readln( Tempstr );  { get input from user }
  119.  
  120.             IF Length( Tempstr ) = 0 THEN
  121.                BEGIN
  122.                   Tempval := Def;  { use default value }
  123.                   Done := TRUE;    { and fall thru }
  124.                END
  125.  
  126.             ELSE
  127.  
  128.                BEGIN
  129.                   Tempval := Val( Tempstr );      { convert string to real }
  130.                   IF (Tempval >= Low) AND (Tempval <= High) THEN
  131.                      Done := TRUE         { All is well, continue }
  132.                   ELSE
  133.                      BEGIN
  134.                         Write( '* Value outside range: ' );
  135.                         Writeln( Low, ' - ', High );
  136.                         Writeln;
  137.                         Done := FALSE        { otherwise go again... }
  138.                      END;
  139.                END;
  140.  
  141.          END
  142.  
  143.       UNTIL Done;
  144.       Input := Tempval;                { and return the value...}
  145.    END;
  146.  
  147.  
  148.