Q(uick)BASIC Statement: LINE INPUT
Quick View
LINE INPUT
A device I/O statement that inputs an entire line (up to 255 characters) to a string variable, without the use of delimiters
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
- LINE INPUT[;] ["promptstring";] stringvariable
Description/Parameter(s)
The promptstring is a string constant displayed on the screen before input is accepted. A question mark is not printed unless it is part of the promptstring. All input from the end of promptstring to the carriage return is assigned to stringvariable.
A semicolon immediately after the LINE INPUT statement keeps the cursor on the same line after the user presses ENTER.
LINE INPUT uses the same editing characters as INPUT.
Example
The following program enables the user to enter text in a notes file. The LINE INPUT statement allows you to enter any characters, including those (such as a comma) that are delimiters in a regular INPUT statement.
'Opens and writes lines to a notes file until you
'enter a blank line.
DO
CLS
PRINT "Enter text. To stop, press <RETURN> without ";
PRINT "entering any new text." : PRINT
OPEN "NOTES.TXT" FOR OUTPUT AS #1
' Take lines until a blank line is entered.
DO
LINE INPUT "->";Inline$
IF Inline$ <> "" THEN PRINT #1, Inline$
LOOP WHILE Inline$ <> ""
CLS : CLOSE #1
' Echo the notes back and see if they are correct.
OPEN "NOTES.TXT" FOR INPUT AS #1
PRINT "You entered: " : PRINT
DO WHILE NOT EOF(1)
LINE INPUT #1, Inline$
PRINT Inline$
LOOP
CLOSE #1
PRINT : INPUT "Is this correct (Y/N)"; R$
LOOP WHILE UCASE$(R$)="N"
END
See also:
Syntax
- LINE INPUT [;] ["promptstring";] stringvariable
Description/Parameter(s)
- A semicolon immediately after the LINE INPUT keywords keeps the cursor on the same line after the user presses Enter.
- The argument promptstring is a string constant displayed on the screen before input is accepted. A question mark is not printed unless it is part of promptstring. All input from the end of promptstring to the carriage return is assigned to stringvariable.
Usage Notes
- LINE INPUT uses the same editing characters as INPUT.
Example
This example uses the DEF SEG statement, the PEEK function, and POKE statement to turn the Caps Lock key on and off. The program uses the LINE INPUT statement to prompt the user to enter a string, demonstrating that the Caps Lock key is on.
Note: This program contains hardware-specific instructions. It works correctly on IBM PC, XT, and AT computers.
DECLARE SUB CapsOn ()
DECLARE SUB CapsOff ()
DECLARE SUB PrntMsg (R%, C%, M$)
CLS
CapsOn
PrntMsg 24, 1, "<Caps Lock On>"
LOCATE 12, 10
LINE INPUT "Enter a string (all characters are caps): ", S$
CapsOff
PrntMsg 24, 1, " "
PrntMsg 25, 1, "Press any key to continue..."
DO WHILE INKEY$ = "": LOOP
CLS
END
SUB CapsOff STATIC
'Turn Caps Lock off.
DEF SEG = 0
'Set Caps Lock off (turn off bit 6 of &H0417).
POKE &H417, PEEK(&H417) AND &HBF
DEF SEG
END SUB
SUB CapsOn STATIC
'Turn Caps Lock on.
'Set segment to low memory.
DEF SEG = 0
'Set Caps Lock on (turn on bit 6 of &H0417).
POKE &H417, PEEK(&H417) OR &H40
'Restore segment.
DEF SEG
END SUB
SUB PrntMsg (Row%, Col%, Message$) STATIC
'Print message at Row%, Col% without changing cursor.
'Save cursor position.
CurRow% = CSRLIN: CurCol% = POS(0)
LOCATE Row%, Col%: PRINT Message$;
'Restore cursor.
LOCATE CurRow%, CurCol%
END SUB
See also: