QBasic/Basic Input

From Wikibooks, the open-content textbooks collection

< QBasic
Jump to: navigation, search

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] ' Prints questionmark before prompt
or
INPUT "[text to user]", [variable] ' No additional question mark

Example:

INPUT "What is your name"; NAME$

When an semicolon (;) is used between the text to the user and the variable a question mark and a space is added. When a comma (,) is used no question mark is added.

The stored variable can now be used in output to the user:

INPUT "What is your name"; NAME$
PRINT "Hello, "; NAME$; "! ";
INPUT "How old are you"; age$
PRINT "The user, "; NAME$; " is "; age$; " years old"

Please note: The string will only store what the user enters, so you must make sure that when programming, you add spaces inside the quotation marks (as shown above) so that it reads properly. Not inserting spaces will concatenate the two words.

The input statement can be used with numerical variables, by automatically converting the string into the number. For example, you can prompt the age directly into an integer:

INPUT "Hello, "; NAME$; "! How old are you"; age

When using an integer with the input statement, any character that can't be understood as a number will print a "Redo from start" error message to the user, and retry the prompt.

Please note: The concatenation rules for a number differ from a string. While a string does not create spaces, a number will insert a space before and after itself when being printed.

Finally, the LINE INPUT command will read a line of text and place the result into a string. While this may seem to be the same command, the INPUT statement will parse commas as a delimiters between fields when multiple variables are passed to it.