Basic Input and Output in QBasic
When developing applications or programs in QBasic, understanding how to handle input and output is fundamental to making your programs interactive and user-friendly. The two primary commands for managing input and output in QBasic are INPUT and PRINT. In this article, we will explore both of these commands in detail, offering several examples to illustrate how they work.
Using the PRINT Command
The PRINT statement is used to display information to the user. It can be used to output text, numeric values, or a combination of both. Here’s how you can use the PRINT command effectively.
Basic Syntax
The simplest form of the PRINT statement looks like this:
PRINT "Hello, World!"
In this example, when the program runs, it will display:
Hello, World!
Printing Numeric Values
You can also print numbers using the PRINT command. For instance:
DIM x AS INTEGER
x = 42
PRINT "The value of x is:"; x
This will output:
The value of x is: 42
Concatenating Strings and Variables
You can combine text and variables in your output. This is done using a semicolon (;) or a comma (,):
- The semicolon keeps the text together on one line.
- The comma will create a tab space between outputs.
Example:
DIM score AS INTEGER
score = 95
PRINT "Your score is:"; score
PRINT "Good job!"; "You passed!"
Output:
Your score is: 95Good job!You passed!
Using a comma:
PRINT "Your score is:", score
This provides a neater output:
Your score is: 95
Formatting Output
QBasic allows for further control over how numbers are displayed. You can format numbers using the Using clause in PRINT:
DIM price AS SINGLE
price = 123.456
PRINT USING "$###.##"; price
This command formats the price to appear as:
$123.46
Here, the number is rounded to two decimal places, and the dollar sign is included.
Taking User Input with the INPUT Command
The INPUT command is how you take user input in QBasic. It allows you to gather information directly from the user.
Basic Syntax
To use the INPUT statement, you would typically do the following:
DIM name AS STRING
INPUT "Enter your name: ", name
In this example, the program will ask for the user’s name, then store it in the variable name.
Collecting Numeric Input
Similarly, you can collect numeric input:
DIM age AS INTEGER
INPUT "Enter your age: ", age
If the user inputs 25, it will be stored in the variable age.
Multiple Inputs
You can gather multiple inputs in a single INPUT statement:
DIM firstName AS STRING, lastName AS STRING
INPUT "Enter your first name and last name: ", firstName, lastName
User input like John Doe will assign John to firstName and Doe to lastName.
Input Validation
While QBasic doesn’t have built-in features for input validation, you can still manually check if the input is of the expected type. For example, if you want to ensure the user enters a number, you could do this:
DIM number AS INTEGER
ON ERROR GOTO InvalidInput
INPUT "Enter a whole number: ", number
GOTO EndProgram
InvalidInput:
PRINT "Please enter a valid whole number."
RESUME
EndProgram:
PRINT "You entered "; number
This program prompts for input and responds appropriately if the input is invalid.
Combining INPUT and PRINT
One of the best practices is combining both PRINT and INPUT commands to make your program more interactive. Imagine creating a simple program that greets a user after taking their name as input:
DIM userName AS STRING
INPUT "Please enter your name: ", userName
PRINT "Hello, "; userName; "! Welcome to the QBasic programming world!"
When the user runs this program and inputs Alice, the output will be:
Hello, Alice! Welcome to the QBasic programming world!
Using INPUT with Arrays
Handling input with arrays can be very useful in certain scenarios. For instance, let’s say you want to collect multiple scores from a user:
DIM scores(5) AS INTEGER
FOR i = 1 TO 5
INPUT "Enter score "; i; ": ", scores(i)
NEXT i
PRINT "You have entered the following scores:"
FOR i = 1 TO 5
PRINT "Score "; i; ": "; scores(i)
NEXT i
In this example, the program will loop through 5 iterations, prompting the user to enter scores and then displaying them all afterward.
Conclusion
Mastering the INPUT and PRINT commands is crucial for any QBasic programmer. These commands enhance user interaction, provide feedback, and display essential information within your programs. With practice, you’ll be able to create more complex interfaces and applications, making your programming journey both enjoyable and rewarding.
If you’re looking to deepen your understanding further, try creating your own interactive programs using a combination of these commands. Test different scenarios, manage arrays, and think about how to organize user input effectively. Happy coding!