Hardware Reference
In-Depth Information
using the
VAL
function. The resulting integer is then tested to see whether it
lies within the acceptable range. If the integer is within range, the procedure is
exited (via
EXIT SUB
) with
num%
containing a valid integer input. If the integer
is not within range, the user is warned and prompted for further input. A similar
routine can be produced for floating point input and, if desired, the prompt string
can be included in the list of parameters to be passed into the function.
String inputs
The simple method of dealing with string input involves using a BASIC
statement of the form:
INPUT "Filename"; n$
This line of code is fortunately not quite so prone to problems as its equivalent
for numeric input. It is, however, worth considering what action we should take
if the user should default the input (i.e. just presses RETURN or ENTER) or
proceeds to input an unacceptably long string the latter is an important consid-
eration when dealing with filenames). Hence our general-purpose string input
routine should allow for the substitution of a default string and should also
truncate the user's input to a specified length. The procedure call might take the
following form:
prompt$ = "Filename"
CALL Stringin(prompt$, 8, "MYSAMPLE", inputstr$)
while the procedure itself would be coded along the following lines:
REM General purpose string input
SUB Stringin(prompt$, length%, default$, inputstr$)
PRINT prompt$;"? ";
LINE INPUT r$
IF r$ = "" THEN r$ = default$
inputstr$ = LEFT$(r$, length%)
END SUB
As before, the procedure prints the prompt string (
prompt$
) and assigns the
user's input to a string variable. The use of
LINE INPUT
(rather than just
INPUT
)
ensures that the user can include punctuation. The user's response (
r$
) is then
checked to determine whether it is a null string (i.e. the user has defaulted) and,
ifso, the specified default string is substituted. Lastly, the string is truncated to
the specified length using the
LEFT$
string function.
The following gives typical user entries and resulting values returned to the
main program (in
inputstr$
) by the foregoing code when length% takes the
value 8:
User input
Value returned
OLD_DATA
OLD_DATA
NEW_SAMPLE
NEW_SAMP
CONTROL_DATA
CONTROL_
(default)
MYSAMPLE