home *** CD-ROM | disk | FTP | other *** search
- { -----------------------------------------------------------------------------
-
- NOTICE:
-
- THESE MATERIALS are UNSUPPORTED by OSS! If you do not understand how to
- use them do not contact OSS for help! We will not teach you how to
- program in Pascal. If you find an error in these materials, feel free
- to SEND US A LETTER explaining the error, and how to fix it.
-
- THE BOTTOM LINE:
-
- Use it, enjoy it, but you are on your own when using these materials!
-
-
- DISCLAIMER:
-
- OSS makes no representations or warranties with respect to the contents
- hereof and specifically disclaim all warranties of merchantability or
- fitness for any particular purpose. This document is subject to change
- without notice.
-
- OSS provides these materials for use with Personal Pascal. Use them in
- any way you wish.
-
- -------------------------------------------------------------------------- }
-
-
- {************************************************************************
-
- INPUT.PAS - function takes a high, low, and default number
- and a prompt and returns when satisfied
- 10-14-86 MJC
-
- INPUT( prompt, low, high, def )
-
- Example:
-
- Int_var := Input( 'Enter number from 1 to 100 ', 1, 100, 0 );
-
- would prompt the user for a number, wait for a response,
- check for proper values, and then return. If a proper value (between
- the Low and High parameter) is not entered, the routine will ask for
- a new response.
-
- Include in your program (*$I input.pas *)
-
- ************************************************************************}
-
-
- { val - Convert the number contained in the string 's' to an integer, and
- return that integer as the function result. We are assuming the caller
- has ensured the string is a valid number, so we we're just going to
- convert characters into the number until a non-digit is encountered, or
- the end of the string is reached. A zero is returned as the function
- value if the string that was passed has zero length. }
-
- FUNCTION val( s: STRING ): integer;
-
- VAR
- { Flag to indicate the number has a leading minus sign }
- minus: boolean;
- i, n : Integer;
-
- BEGIN
- { Start with the first character of the string, but first skip leading
- blanks. }
- i := 1;
- WHILE (i < length(s)) AND (s[i] = ' ') DO
- i := i + 1;
- { If there are characters still in the string, convert it to a number }
- n := 0;
- IF length(s) >= i THEN
- BEGIN
- { If first char is '-', we have to negate the number after we finish
- converting the digits. }
- IF s[i] <> '-' THEN
- minus := false
- ELSE
- BEGIN
- minus := true;
- i := i + 1;
- END;
- { While there are more digits in the string, convert characters. }
- WHILE (i <= length(s)) AND (s[i] IN ['0'..'9']) DO
- BEGIN
- n := (n * 10) + ord(s[i]) - ord('0');
- i := i + 1;
- END;
- { Negate the final result, if necessary. }
- IF minus THEN
- n := -n;
- END;
- { Return the converted number as the result of this function. }
- val := n;
- END;
-
-
- {**************************************************************************
-
- The famous Input routine...
-
- ************************************************************************}
-
- FUNCTION Input( Prompt : STRING; Low, High, Def : INTEGER ) : INTEGER;
-
-
- VAR
-
- Tempval : INTEGER; { hold temp value... }
- Tempstr : STRING; { get input }
- Done : BOOLEAN; { Are we done... }
-
- BEGIN
-
- REPEAT
- BEGIN
- Write( Prompt ); { send prompt string }
- Readln( Tempstr ); { get input from user }
-
- IF Length( Tempstr ) = 0 THEN
- BEGIN
- Tempval := Def; { use default value }
- Done := TRUE; { and fall thru }
- END
-
- ELSE
-
- BEGIN
- Tempval := Val( Tempstr ); { convert string to real }
- IF (Tempval >= Low) AND (Tempval <= High) THEN
- Done := TRUE { All is well, continue }
- ELSE
- BEGIN
- Write( '* Value outside range: ' );
- Writeln( Low, ' - ', High );
- Writeln;
- Done := FALSE { otherwise go again... }
- END;
- END;
-
- END
-
- UNTIL Done;
- Input := Tempval; { and return the value...}
- END;
-
-
-