Beginner's Programming Tutorial in QBasic

(Kiana) #1

Retrieving keyboard input from the user


One way to receive input from the keyboard is with the INPUT command. The INPUT command
allows the user to enter either a string or a number, which is then stored in a variable.
INPUT data$
PRINT data$
When this program is executed, the INPUT command displays a question mark, followed by a
blinking cursor. And when you enter text, the program stores that text into the variable data$,
which is printed to the screen.


TIP: If you place a string and a semi-colon
between INPUT and the variable, the program
will print the string.
INPUT "Enter some text:"; data$

To receive a number, use a non-string variable.
INPUT number
PRINT number
If you enter text instead of a number, the QBasic interpreter displays an error message ("Redo
from start").


Below is another example of the INPUT command:
PRINT "Enter some text:"
INPUT text$
PRINT "Now enter a number:"
INPUT num
PRINT text$
PRINT num


TIP: on the previous line by using a semi-colon. You can have the question mark displayed
PRINT "Enter some text:";
INPUT text$
Free download pdf