Hardware Reference
In-Depth Information
PRINT "Are you sure? (Y/N)"
DO
DO
r$ = INKEY$
LOOP WHILE r$ = ""
IF r$ "y" OR r$ = "Y" THEN f% = 1
IF r$ = "n" OR r$ = "N" THEN f% = 0
LOOP WHILE f% <> 1 AND f% <> 0
FNConfirm% = f%
END DEF
...
REM Main program starts here
...
...
IF FNConfirm% = 1 THEN ... ELSE ...
...
...
The function returns a flag, f% , which is true (non-zero) if the user presses
Y or y and is false (zero) if the user presses N or n. The function waits until a
character is available from the keyboard (via the inner DO ... LOOP ) and then
checks to ensure that it is one of four acceptable responses (i.e. upper and lower
case Y and N). Any other keyboard input is invalid and the program continues
to wait for further keyboard input until an acceptable value is returned (during
this time the prompt message remains on the screen and does not scroll).
When a valid input is received, the function returns the appropriate flag in
f% . It is important to note that INKEY$ , unlike INPUT , does not require the use
of the RETURN or ENTER key as a terminator and that a function definition must
always precede the main body of code which calls it.
The problem could equally well have been solved by means of a procedure
(rather than a user-defined function). In this case the code would have been as
follows:
REM Main program starts here
DECLARE SUB Confirm(f%)
...
...
CALL Confirm(f%)
IF f% = 1 THEN . . . ELSE.
...
...
REM Confirm procedure
SUB Confirm(f%)
r$=""
f%=-1
PRINT "Are you sure? (Y/N)"
DO
DO
r$ = INKEY$
LOOP WHILE r$ = ""
IF r$ = "y" OR r$ = "Y" THEN f% = 1
IF r$ = "n" OR r$ = "N" THEN f% = 0
LOOP WHILE f% <> 1 AND f% <> 0
END SUB
Search WWH ::




Custom Search