QBasic/Basic Input
The INPUT command is used to gather input from the user. This section will attempt to teach you how to gather input upon request from the user. For real-time input, see QBasic/Advanced Input.
Here is the syntax of the input command:
INPUT "[text to user]"; [variable] ' Question mark added
or
INPUT "[text to user]", [variable] ' No question mark added
Example:
INPUT "What is your name"; name$
or
INPUT "What is your age", age
When a semicolon (;) is used after the text output to the user, a question mark (?) and space ( ) are added to the output. When a comma (,) is used, no question mark is added.
If a string is specified (e.g., 'name$'), anything the user enters before pressing the 'return' key will be accepted.
If a numeric variable (e.g., 'age') is specified, the user must enter a number. If any non-numeric key is entered, the error message "Redo from start" will be output and the INPUT command rerun.
6INPUT.BAS
[edit | edit source]CLS
INPUT "What is your name"; name$
PRINT "Hello, "; name$;
INPUT "How old are you"; age
INPUT "What is your best computer game?", game$
PRINT " name:"; name$
PRINT " age:"; age; " years old"
PRINT "best game:"; game$
Please note: In the PRINT command, the (;) function concatenates (joins) the contents of the string variables with the text between the quotes (" "). Note the use of spaces so that the final printed text reads properly.
If a numerical variable is specified within the PRINT command, additional space is automatically added both before and after the number.
See also: LINE INPUT
command to read a line of text from a file (and place the result in a string variable) or to input a series of variables (in which case any comma found will be treated as a delimiter between fields).
INPUT # and LINE INPUT
[edit | edit source]INPUT # uses an open file stream to collect data from the file itself. The file may be a data file, a bitmap, or a text file. The syntax is:
INPUT #file_stream, variable1 ; variable2$ ' more variables can be taken.
LINE INPUT is used to collect an entire line of a text file. Syntax:
LINE INPUT 1,file_line '1 is the file stream number. Can be any other number too.
WARNING:
If input is taken beyond the file end, the error : "Input past end of file " is issued.
You can use LOF
and EOF
functions to prevent errors.
(LOF stands for LENGTH OF FILE while EOF stands for END OF FILE)