Q(uick)BASIC Statement: INPUT

Quick View

INPUT

A device I/O statement that reads input from the keyboard during program execution and stores it into a list of variables

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • INPUT [;] ["prompt"{; | ,}] variablelist
  • LINE INPUT [;] ["prompt";] variable$
  • INPUT #filenumber%, variablelist
  • LINE INPUT #filenumber%, variable$
Description/Parameter(s)
prompt An optional literal string that is displayed before the user enters data. A semicolon after prompt appends a question mark to the prompt string.
variablelist One or more variables, separated by commas, in which data entered from the keyboard or read from a file is stored. Variable names can consist of up to 40 characters and must begin with a letter. Valid characters are A-Z, 0-9, and period (.).
variable$ Holds a line of characters entered from the keyboard or read from a file.
filenumber% The number of an open file.
  • INPUT uses a comma as a separator between entries. LINE INPUT reads all characters up to a carriage return.
  • For keyboard input, a semicolon immediately after INPUT keeps the cursor on the same line after the user presses the Enter key.
Example
CLS OPEN "LIST" FOR OUTPUT AS #1 DO INPUT " NAME: ", Name$ 'Read entries from the keyboard. INPUT " AGE: ", Age$ WRITE #1, Name$, Age$ INPUT "Add another entry"; R$ LOOP WHILE UCASE$(R$) = "Y" CLOSE #1 'Echo the file back. OPEN "LIST" FOR INPUT AS #1 CLS PRINT "Entries in file:": PRINT DO WHILE NOT EOF(1) LINE INPUT #1, REC$ 'Read entries from the file. PRINT REC$ 'Print the entries on the screen. LOOP CLOSE #1 KILL "LIST"
Syntax
  • INPUT[;]["promptstring"{;|,}]variablelist
Description/Parameter(s)
Argument Description
; A semicolon immediately after INPUT keeps the cursor on the same line after the user presses ENTER.
promptstring A string constant printed before the prompt character.
; Prints a question mark at the end of the promptstring.
, Prints the promptstring without a question mark.
variablelist A list of variables, separated by commas, to accept the input values. See the discussion below.

The INPUT statement causes the program to pause and wait for data. You can then enter the required data at the keyboard.

The data that you enter is assigned to the variables in variablelist. The number of data items that you supply must be the same as the number of variables in the list. The first character encountered after a comma that is not a space, carriage return, or line feed is assumed to be the start of a new item.

The variable names in the list may be numeric- or string-variable names (including subscripted variables), array elements, or elements of records. The type of each data item that you input must agree with the type of the variable. (Strings input to an INPUT statement need not be surrounded by quotation marks.) If this first character is a quotation mark ("), the string item will consist of all characters read between the first quotation mark and the second. This means a quoted string may not contain a quotation mark as a character. If the first character of the string is not a quotation mark, the string is an unquoted string and terminates on a comma, carriage return, or line feed.

Input stored in elements of a record must be input as single elements:

  • TYPE Demograph
  • FullName AS STRING * 25
  • Age AS INTEGER
  • END TYPE
  •  
  • DIM Person AS Demograph
  • INPUT "Enter name and age: ";Person.FullName,Person.Age

Responding to an INPUT statement with too many or too few items, or with the wrong type of value (for example, numeric instead of string), produces an error message which reads "Redo from start."

No assignment of input values is made until you give an acceptable response.

It is possible to edit a line of input before you press ENTER. The following list describes the key combinations that allow you to move the cursor, delete text, and insert text on the input line:

Keys Action commands, INPUT statement
CTRL+  or RIGHT Moves cursor one character to the right.
CTRL+] or LEFT Moves cursor one character to the left.
CTRL+F or CTRL+RIGHT Moves cursor one word to the right.
CTRL+B or CTRL+LEFT Moves cursor one word to the left.
CTRL+K or HOME Moves cursor to the beginning of the input line.
CTRL+N or END Moves cursor to the end of the input line.
CTRL+R or INS Toggles insert mode on and off. When insert mode is on, characters above and to the right of the cursor are shifted to the right as new characters are entered.
CTRL+I or TAB Tabs right and inserts (insert mode on), or overwrites (insert mode off).
DEL Deletes the character at the cursor.
CTRL+H or BACKSPACE Deletes the character to the left of the cursor, unless the cursor is at the beginning of the input, in which case it deletes the character at the cursor.
CTRL+E or CTRL+END Deletes to the end of the line.
CTRL+U or ESC Deletes entire line, regardless of cursor position.
CTRL+M or RETURN Stores input line.
CTRL+T Toggles function key label display on and off at bottom of screen.
CTRL+BREAK or CTRL+C Terminates input (exits compiled program).

Other Uses of the INPUT Keyword

LINE INPUT - to read an entire line of input from the keyboard and store it in one string variable
INPUT # - to retrieve data items (or "fields") from a sequential file record and store them into a list of variables
LINE INPUT # - to retrieve an entire record from a sequential file and store it into one string variable
Example

This example uses INPUT to calculate the area of a circle from a radius you supply. It prompts you for a new radius until you enter 0.

CLS PI = 3.141593 : R = -1 DO WHILE R PRINT "Enter radius (or 0 to quit)." INPUT ; "If radius = ", R IF R > 0 THEN A = PI * R ^ 2 PRINT ", the area of the circle ="; A END IF PRINT LOOP

Sample Output:

Enter radius (or 0 to quit). If radius = 3, the area of the circle = 28.27434 Enter radius (or 0 to quit). If radius = 4, the area of the circle = 50.26549 Enter radius (or 0 to quit). If radius = 0

See also:

Syntax
  • INPUT [;] ["promptstring"{;|,}] variablelist
Description/Parameter(s)
[;] Determines screen location of the text cursor after the program user enters the last character of input:
  • ";" keeps cursor at next character position;
  • if the semicolon is omitted, the cursor skips to next line.
["promptstring"] A literal string that will be displayed on the screen before the program user enters DATA items into the data input field.
{;|,}
  • ";" appends a question mark to promptstring;
  • "," does not.
variablelist An ordered list of variables which will hold the DATA items as they are entered.
  • The argument variablelist is made up of one or more variable names, each separated from the next by a comma. Each name may refer to:
    • A simple numeric variable.
    • A simple string variable.
    • A numeric or string array element.
    • A record element.

Optimization Notes

  • If the user will be inputting only decimal integers, you can save between 1.6K and 11K in the size of a non-stand-alone .EXE file by linking with the stub file NOFLTIN.OBJ. This places the following restrictions on user input:
    • Decimal numbers only (no leading &H, &O, or & base specifiers).
    • No trailing type specifiers (%, &, !, #, @, and $).
    • No decimal point, E, or D (for example,1.E2 cannot be used instead of the integer 100).
  • If the user can be limited to using the Enter and Backspace keys for editing, you can reduce the size of the .EXE file by linking the stub file NOEDIT.OBJ.

Usage Notes

  • The INPUT statement causes a running program to pause and wait for the user to enter data from the keyboard. The number and type of data items required from the user is determined by the structure of variablelist.
  • The number of entered data items must be the same as the number of variables in the list. The type of each entered data item must agree with the type of the variable. If either of these rules is violated, the program will display the prompt, "Redo from start" and input values aren't assigned.
  • The INPUT statement determines the number of entered data items in a multiple-item list this way:
    • The first character encountered after a comma that is not a space, carriage return, or line feed is assumed to be the start of a new item.
    • If this first character is a quotation mark ("), the item is typed as string data and will consist of all characters between the first quotation mark and the second.
    • Not all strings have to start with a quotation mark, however. If the first character of the string is not a quotation mark, it terminates on a comma, carriage return, or line feed.
  • Input stored in a record must be entered as single elements. For example:

  • TYPE Demograph
  • FullName AS STRING * 25
  • Age AS INTEGER
  • END TYPE
  • DIM Person AS Demograph
  • INPUT "Enter name and age: "; Person.FullName, Person.Age

  • You may want to instruct the user on how it is possible to edit a line of input before pressing the Enter key to submit the line to the program:
Keystrokes Edit function
Ctrl+M or Enter Store input line.
Ctrl+H or Backspace Delete the character to the left of the cursor, unless the cursor is at the beginning of the input, in which case it deletes the character at the cursor.
Ctrl+\ or Right Arrow Move cursor one character to the right.
Ctrl+] or Left Arrow Move cursor one character to the left.
Ctrl+F or Ctrl+Right Arrow Move cursor one word to the right.
Ctrl+B or Ctrl+Left Arrow Move cursor one word to the left.
Ctrl+K or Home Move cursor to beginning of input line.
Ctrl+N or End Move cursor to end of input line.
Ctrl+R or Ins Toggle insert mode on and off.
Ctrl+I or Tab Tab right and insert (insert mode on), or tab right and overwrite (insert mode off).
Del Delete the character at the cursor.
Ctrl+E or Ctrl+End Delete to the end of the line.
Ctrl+U or Esc Delete entire line, regardless of cursor position.
Ctrl+T Toggle function key label display on and off at bottom of screen.
Ctrl+C or Ctrl+Break Terminate input (exit compiled program).
  • When you use INPUT with ISAM programs, BASIC performs a CHECKPOINT operation every 20 seconds during an INPUT polling loop. A CHECKPOINT writes open database buffers to disk.
Example

This example uses the INPUT statement to prompt the user for a value and assign it to a variable. The program calculates the area of a circle.

CLS PI = 3.141593: R = -1 DO WHILE R PRINT "Enter radius (or 0 to quit)." INPUT ; "If radius = ", R IF R > 0 THEN A = PI * R ^ 2 PRINT ", the area of the circle ="; A END IF PRINT LOOP

Sample Output:

Enter radius (or 0 to quit). If radius = 3, the area of the circle = 28.27434 Enter radius (or 0 to quit). If radius = 4, the area of the circle = 50.26549 Enter radius (or 0 to quit). If radius = 0