Terminal Input

Use the INPUT statement to request terminal input.

The INPUT Statement

In the simplest form of terminal input, the user can be prompted for a value for the variable ANSWER with the statement:

INPUT ANSWER

This statement will print the prompt character on the screen and wait for terminal input at that position. The user can type a response and press ENTER for the response to be accepted. (The prompt character can be reassigned with the PROMPT statement.)

Variations on INPUT

There are several variations to the INPUT statement. For example, suppose that the answer the user is prompted for can be only Y for yes or N for no. The INPUT statement can specify that only one character is expected, with the statement:

INPUT ANSWER,1

The maximum number of characters that will be accepted as input is 1. When 1 character is typed by the user, the program will assume that the input is complete and continue execution immediately, without waiting for a carriage return. Any positive integer can be used in an INPUT statement as the maximum length of input, up to the size of the input buffer (140).

To force the program to wait for a carriage return before accepting the response, an underscore (_) can be placed at the end of the INPUT statement. By using the underscore, the program will send a beep to the terminal if the user tries to type in more than the maximum number of characters, but it will wait for a carriage return before accepting the input. Thus the user is given a chance to verify the response before continuing with the program.

In addition, the INPUT statement can be used to print data in the field to be written. The field might contain a default answer, to be accepted (by pressing ENTER) or to be reassigned (by backspacing, typing the new answer, and pressing ENTER). The field might also be filled by a fill character, showing (for example) 5 asterisks when a 5-digit zip code is requested. The only valid fill characters are zeros and asterisks.

A FROM clause is supplied with the INPUT statement, for accepting input from a remote line. See Communications later in this section for more information on receiving input from a remote line.

Input from the Type-ahead Buffer (INPUTIF)

The INPUTIF statement accepts input only from the type-ahead buffer. The type-ahead buffer contains characters which have been typed between input requests, to be supplied as a response to the next input request.

The INPUTIF statement follows the same syntax as the INPUT statement, except that it requires either a THEN or ELSE clause (or both). If there is at least one character in the type-ahead buffer, the program will wait for a carriage return if one was not already in the type-ahead, and the THEN statements will be executed. If the type-ahead is empty, the ELSE clause will be instantly executed: the INPUTIF statement will not wait for text if the type-ahead is empty.

Example

If a program expects a response within thirty seconds, the source code might read:

SLEEP 30

INPUTIF RESPONSE ELSE

STOP

END

The TA function returns the number of characters in the type-ahead buffer for the specified line. In addition, the TA statement can toggle the type-ahead feature on and off, or clear the type-ahead buffer of the current process. See Communications later in this section for more information on the type-ahead feature.

Masked Input Statements (INPUT @)

The INPUT @ statement combines two invaluable enhancements to the INPUT statement: it accepts screen coordinates for the input string, and it allows a mask to be applied directly on the input.

The INPUT @ statement takes screen coordinates as arguments, and places the cursor (printing the prompt character) at that position. Once the response has been supplied, the INPUT @ statement compares it and translates it against the mask (if any). If the response does not match the mask, an error message is printed at the last line of the screen and the user is prompted again. The input data is then converted into internal format for storage.

For example, take the case of a screen-formatted program which prompts for a date and then translates it into internal format. Using the standard INPUT statement, the program would have to use a loop to place the cursor at the right position, prompt for the input, test for every way a date can be written (JUNE 4 1965, 4 JUN 1965, 6/4/65, 06/04/1965, 6-4-65, etc.), and then convert it to internal format. However, with the INPUT @ statement, the programmer can simply write:

INPUT @(14,10) BIRTHDATE "D"

 

D

Specifies an internal date conversion is performed.

See Sending Output to the Screen and Printer for more information on data masking.

INPUTTRAP, INPUTNULL and INPUTERR

Several statements were designed to be used concurrently with INPUT @.The INPUTTRAP and INPUTNULL statements were designed to provide escapes from the internal loop of INPUT @. The INPUTNULL statement specifies a character which will be interpreted as the null string by INPUT @. The INPUTTRAP statement allows the programmer to specify characters which, if supplied as answers to INPUT @, will branch to another statement label. Using INPUTTRAP, the operator can be told, in a menu program for example, that pressing ESC at any prompt will exit the program, and pressing CTRL+Z will return to the main menu.

In addition, the INPUTERR statement prints a message on the last line of the screen. This message will be cleared when correct input is taken by a succeeding INPUT @ statement. The INPUTERR statement can be used to print a message about what sort of input is expected, or it can be used in a loop with INPUT @ if a response requires further testing.

Example

If the programmer requires a date within the next year, the code might read:

TODAY=DATE( )

VALID = 0

INPUTERR "PLEASE ENTER A DATE WITHIN THE NEXT YEAR"

LOOP

INPUT @(14,10) RES.DATE "D"

BEGIN CASE

CASE RES.DATE < TODAY

INPUTERR "INVALID INPUT. PLEASE ENTER A

FUTURE DATE."

CASE RES.DATE > TODAY + 365

INPUTERR "PLEASE ENTER A DATE WITHIN THE NEXT YEAR."

CASE 1

VALID = 1

END CASE

UNTIL VALID DO REPEAT

See Sending Output to the Screen and Printer for more information both on data masking and on formatted screens.

INPUT and the Data Stack

If the data stack is not empty, its contents will be supplied to any terminal input statement, and the user will not be prompted. The data stack is assigned with the DATA statement. The SYSTEM(10) function can be used to determine whether the data stack is empty.

See External Program Control for more information about the data stack.

See Also

Overview of mvBASIC Statements and Functions

Assignment Statements

Intrinsic Functions

Internal Program Control

External Program Control

Sending Output to the Screen and Printer

Dynamic Array Processing

Generalized String Processing

Dimensioned Arrays

Reading and Updating File Items

Reading and Writing Tapes or Floppy Disks

Communications

Execution Locks

Compiler Directives

Miscellaneous Statements and Functions

The Error Message Processor