INPUT

From BeebWiki
Jump to: navigation, search

INPUT is a statement that takes one or more values from the user when the program is run.

INPUT
Availability Present in all original versions of BBC BASIC.
Syntax BASIC I INPUT [LINE] {,|'|SPC <numeric>|TAB(<numeric>[,</code><numeric>]<code>)|<string-const>|<string-var>|<num-var>}
BASIC II-V INPUT [LINE] {;|,|'|SPC <numeric>|TAB(<numeric>[,<numeric>])|<string-const>|<string-var>|<num-var>}
Token (hex) BASIC I-V E8 (statement)
Description BASIC I-V Prints one or more prompts and waits for the user to type a string and press RETURN after each prompt, then parses the typed string and stores the parsed values in the named variables in sequence.
Associated keywords COUNT, GET, INKEY, PRINT, SPC, TAB, VAL

Description

INPUT allows a BASIC program to collect numbers or strings from the user at run time. It does this by waiting for the user to type one or more lines of text and press the RETURN key after each one. BASIC then stores values extracted from the line or lines in a list of variables named by the program.

Each time INPUT waits for the user to type a line, it prints a prompt. By default the prompt is a question mark ?, but the program can substitute a more descriptive reminder of what the user should enter next. The program can format the prompt as well (using a syntax similar to PRINT.)

More than one variable can be input in one INPUT statement. The user can press RETURN between each one, or run them together with commas between them. However it is considered better programming style to prompt, input (and validate) the user's values one by one.

Details

The INPUT sequence is read from left to right. A <string-const> is printed without a following line feed. The ' element causes a line feed, SPC prints a number of spaces, and TAB repositions the cursor as it does in PRINT. All of these suppress the default prompt. In BBC BASIC the elements ; and , have no positioning effect (and do not 'skip' items in the input line) but they generate the default prompt if they are the last element before a variable. The INPUT statement need not include any punctuation even if multiple variables are collected.

The user's input is read from the OSRDCH input stream via the read-line routine OSWORD &00. BASIC permits all ASCII values to be typed but the control characters are intercepted as usual: the RETURN key completes the line, the ESCAPE key aborts input and raises an Escape error, CTRL U erases the current line and DELETE erases one character. All other control characters carry out immediate VDU commands and are not added to the line. The maximum line length is 238 characters excluding the new line.

COUNT is updated while printing the prompts and zeroed each the time the user presses RETURN.

The optional LINE keyword causes INPUT to treat each line of input as a string literal. One line of input is taken for each named variable. A string variable is filled with the complete line as typed, and a numeric variable is assigned the first numeric value in the line (as per VAL).
Without LINE, lines of input are read and parsed until all variables are assigned values. Initial whitespace is skipped; if the first character is then a double quote " then the string literal is escaped and the variable assigned as above, and any text between the closing double quote and the next comma or end-of-line is discarded. Otherwise the text from that character to the next comma or end-of-line is used. If there is another variable than parsing restarts after the comma, and if there is no comma then another line of input is read in.

Note that a custom prompt (', SPC, TAB, <string-const>) divides the list of input variables, such that if an excess of items has been input they are discarded and not used to fill variables on the right hand side.

Examples

>INPUT temp
?37.5
>PRINT temp
      37.5

>INPUT X%,Y%
?640
?512
>PRINT X%,Y%
       640       512

>INPUT X%,Y%
?160,256
>PRINT X%,Y%
       160       256

>INPUT '"X coordinate: " X%, "Y coordinate: "Y%

X coordinate: 640,512
Y coordinate: 480
>PRINT X%,Y%
      640        480

>INPUT A$,B$
?"Open, sesame" doesn't work, said Aladdin.
>PRINT A$,B$
Open, sesame        said Aladdin.
>INPUT C$,D$
?What about "Open, sarsaparilla", suggested the genie.
>PRINT C$,D$
What about "Open    sarsaparilla"
>INPUT LINE E$
?"At last", he cried, "the Cave of Wonders!"
>PRINT E$
"At last", he cried, "the Cave of Wonders!"

Regregex 22:42, 12 November 2012 (UTC)