When attempting to perform arithmetic on data entered from the keyboard, you can use the DATATYPE( ) function to check that the data is valid.
This function has several forms. The simplest form returns the word, NUM, if the expression inside the parentheses ( ) is accepted by the interpreter as a number that can be used in the arithmetic operation. Otherwise, it returns the word, CHAR. For example:
The value of DATATYPE(56) is NUM The value of DATATYPE(6.2) is NUM The value of DATATYPE('$5.50') is CHARIn the following procedure, DATATYPE.CMD, the internal REXX function, DATATYPE( ), is used and the user is asked to keep typing a valid number until a correct one is typed.
/* Using the DATATYPE( ) Function */ DO UNTIL datatype(howmuch) = 'NUM' SAY 'Enter a number' PULL howmuch IF datatype (howmuch) = 'CHAR' THEN SAY 'That was not a number. Try again!' END SAY 'The number you entered was' howmuch EXIT
If you want the user to type only whole numbers, you could use another form of the DATATYPE( ) function:
DATATYPE (number, whole)
The arguments for this form are:
o
This form returns a 1 if number is a whole number, or a 0 otherwise.