A Flexible Input Subroutine
Glenn M. Kleiman Research Triangle Park, NC
Many interactive programs require a variety of types of input from the user. For example, in my own programs written for classroom use by children, each of the following four types of input are often required:
- Alphabetic strings, such as the user's name or answers to questions.
- Numbers, such as the user's age or the answers to math problems.
- Single digits or letters from a restricted set, such as when the user is asked to make a selection from a menu.
- Y or N, in response to questions such as "Do you want to continue? (Y/N)".
A program designed for unsophisticated users must have checks that the user's input is appropriate. For example, the programmer must guard against the uncertain user who, when given a Yes/No question, presses M for "maybe". Often, particularly in programs to be used by children, one should also control the number of characters that can be input. A program should not accept a name consisting of 100 letters, nor should it accept 15 digits in answer to a math problem that calls for a 3 digit answer. Furthermore, the user should be able to erase mistakes, and inappropriate responses should not stop program execution.
I have written a general purpose input subroutine to handle all of the above. It is written for the PET, but most of the routine is compatible with other BASICs, so it can be easily revised for other microcomputers.
Within a program using this subroutine, the accepted inputs are specified by assigning values to variables before the subroutine is called. The main variable is UF, which can have any one of four values. If UF = 0 (the default value), any letters, but no other characters, will be accepted. If UF = 1 then only numbers will be accepted. For both letters and numbers, UM controls the maximum number of input characters. The default value for UM is set to 1 in line 300.
In order to restrict the accepted characters, as for menu selection responses, UF is set to 2, and the first and last characters to be accepted are assigned to variables F$ and L$, respectively. For example, the following line in a program will set the subroutine to accept only the letters M, N, O, and P:
UF = 2 : F$ = "M" : L$ = "P" : GOSUB300
Finally, to accept only Y or N, UF is set to 3. If UF = 2 or 3, UM is set to 1 automatically.
In all cases, inappropriate input is ignored. Input characters can be erased by pressing the DEL key and a completed input is signaled by pressing RETURN. DEL and RETURN are not accepted until at least one character has been input. Once UM characters have been input, only DEL and RETURN will be accepted.
When RETURN is pressed, UF and UM are reset to their default values. Input strings are then available in the program as variable IN$, input numerics as IN.
A few other notes. I use a flashing ? as a cursor, but any character can be substituted in line 420. In line 430, UT = TI + 35 controls the rate of cursor flashing. The flashing rate of 35 jiffies is slower than most cursors, but seems to be less annoying to many people than the usual speed. The technique of flashing the cursor is based on the INP routine from CURSOR #4. This subroutine, and any other frequently used one, should be placed at the beginning of the program. The reason is that whenever a GOSUB (or a GOTO) occurs, the sequential search for the referenced line number begins at the first line of the program. An input subroutine placed at the end of a long program may be noticeably slow in accepting responses.
This subroutine, written to be easily readable rather than compact, uses 406 bytes (without the REMs).
100 REM FLEXIBLE INPUT SUBROUTINE 101 REM 102 REM GLENN M. KLEIMAN 103 REM TEACHIHG TOOLS: 104 REM MICROCOMPUTER SERVICES 105 REM P.O. BOX 12679 106 REM RESEARCH TRIANGLE PARK 107 REM N.C. 27709 110 REM 120 REM VARIABLES TO BE SET: 130 REM UF = 0 FOR ALPHABETIC INPUT 140 REM UF = 1 FOR NUMERIC INPUT 150 REM UF = 2 FOR RESTRICTED INPUT 160 REM UF = 3 FOR Y OR N INPUT 170 REM 180 REM IF UF = 0 OR 1 SPECIFY: 190 REM UM = MAXIMUM NUMBER OF INPUT CHARACTERS 200 REM (DEFAULT UM SET IN LINE 330) 210 REM IF UF = 2, SPECIFY: 220 REM F$ = FIRST CHARACTER ACCEPTED 230 REM L$ = LAST CHARACTER ACCEPTED 240 REM 250 REM OUTPUT VARIABLES 260 REM IN$ = INPUT STRING 270 REM IN = VAL (IN$) 280 REM 300 IFUM = 0THENUM = 1 310 IFUF = 0THENF$ = "A" : L$ = "Z" 320 IFUF = 1THENF$ = "0" L$ = "9" 330 IFUF > 1THENUM = 1 340 IN$ = "" : UT = TI : UC = 1 400 GETU$ : IFU$ <> GOTO440 410 IFUT > TI GOTO 400 420 PRINTMID$ (" ?", UC, 1) ; "←" ; 430 UC = 3 - UC : UT = TI + 35 : GOTO 400 440 UL = LEN (IN$) : IFUL = UMGOTO 510 450 IFUF <> 3GOTO 480 460 IFU$ = "Y" ORU$ = "N" GOTO490 470 GOTO 500 480 IFU$ < F$ORU$ > L$GOTO 500 490 IN$ = IN$ + U$ : PRINTU$ ; : GOTO400 500 IFUL = 0 GOTO400 510 IFU$ = CHR$ (20) THENIN$ = LEFT$ (IN$, UL - 1) : PRINT "←←←"; 520 IFU$ <> CHR$ (13) GOTO400 530 PRINT " " : UF = 0 : UM = 0 : IN = VAL (IN$) : RETURN READY.