a [warebiz] :: QBasic - Step By Step :: Section 2 - Input and Output
Make your own free website on Tripod.com
QBasic - Step By Step :: Section 2
Author: Mike Ware
Website: [warebiz] :: "The Programmer's Domain" - http://warebiz.tripod.com
Email: warebiz@yahoo.com
Copyright © 2002 Michael Shawn Ware, All Rights Reserved.

You are here --> [Section 2 :: Input and Output]

"Jump To Articles"
    --> Interacting With the User
    --> Formatting Output Results

Interacting With the User

A program is generally written to "interact with the user". That is, you must get information from the user during program execution and take appropriate action depending on the data the user specifies. Getting information from the user involves using the INPUT statement, which allows the user to enter data during program execution. The actual data the user enters is stored in variables for use in the rest of the program. The INPUT statement has the following format:

    INPUT ["prompt";] variable [, variable2, ...]

The "prompt" and variable2 are placed in brackets because they are optional. The command INPUT followed by a variable is required. Basically, when the INPUT command is executed, the program will pause so the user can input information through the use of a keyboard or other input device. As already mentioned, the data the user enters is stored in variable. You can place many variables after the INPUT statement, but the variables must be seperated by a comma. The user must also enter the data seperated by a comma so the information will be stored in the correct variables.

As previously mentioned, the "prompt" is optional but is highly recommended. A prompt is used to instruct the user of what type of data needs to be entered. You might know what type of data needs to be entered because you wrote the program, but what if someone else tries to run it. That person would have no clue of what to do. He/she would see a blinking cursor on the screen and probably think the program has a bug or is not working properly. When no prompt is used, the user will most likely experience some common errors such as: not inputting information for all variables or trying to put character values into a numeric variable. In both cases, a "redo from start" error message will be displayed and execution will "rewind" back to the statement and start over. So, be sure to provide the user with a very descriptive "prompt" explaining what needs to be entered.

There are two ways to display a prompt to the user:

    1 - issue a PRINT statement and then issue an INPUT statement
    2 - combine the prompt with the INPUT statement

For example, consider the following code which uses the first method for displaying a prompt:

    PRINT "Enter the score for test1, test2, and test3 (seperated by a comma)";
    INPUT test1, test2, test3
    average = (test1 + test2 + test3) / 3
    PRINT "The average of the 3 test scores is "; average

The following code uses the second method for displaying a prompt:

    INPUT "What is your name"; name$
    INPUT "How old are you"; age
    INPUT "What are your 3 favorite colors (seperated by a comma)"; favColor1$, favColor2$, favColor3$

NOTE: By placing a semi-colon after the prompt, the user will see a question mark at the end of the prompt. If you want to remove the question mark, simply use a comma ( , ) instead of a semi-colon to terminate the statement. For example,

    INPUT "Enter the score for test1, test2, and test3 (seperated by a comma): ", test1, test2, test3

Now we can get information from the user, but the info is worthless if we don't know how to use it in the form of output. Read on for more about formatting output...

Formatting Output Results

Formatting your output is very important. You will want your program to display results in a crisp, clean manner. It should be printed in a neat layout and capable of being read and understood easily. Most of the ways you can organize output stems from using the PRINT and PRINT USING statements as well as using the TAB( ) and SPC( ) functions. Let's begin with the PRINT statement.

Using a semi-colon ( ; ) to divide the elements in a PRINT statement instructs the compiler to print the next item in the next available print position (or column of the screen). For example, consider the following output statement:

    PRINT "QBasic"; "Is"; "Easy"

The above statement would produce the following output:


Obviously, you don't want your output to run together like this. You could insert a "blank" space in the strings to seperate them, such as using:

    PRINT "QBasic "; "Is "; "Easy"

You could also use the SPC( ) function, which is described later, to seperate the elements. You should also note that when numeric values are printed using a PRINT statement seperated by a semi-colon, a space is always placed after the value for better readability.

While using a semi-colon to seperate PRINT elements places the elements in the next available column, using a comma ( , ) to seperate the elements places the elements in the next available print zone. Each line in the output screen is divided into sections, which are called print zones. Each print zone is 14 characters (columns) wide. The following specifies the column where each print zone begins:

    zone 1 --> 1
    zone 2 --> 15
    zone 3 --> 29
    zone 4 --> 43
    zone 5 --> 57

If you use a comma to control the output format in a PRINT statement, the next item to be printed will be placed at the beginning of the next print zone. Using this type of output can be very useful when you want to print table headings and other similar items. For example, consider the following:


The would produce output similar to the following:

    NAME        HIGH SCHOOL        GPA

You can also "skip" print zones by using two back-to-back commas in a PRINT statement. For example, consider:


A function that gives you even greater power when it comes to placing items in specified columns is the TAB( ) function. The TAB( ) function allows you to print output in any specified column in the output screen line (row); obviously, this gives you greater flexibility and structure. The parameter in the TAB( ) function may be a numeric constant, variable, or expression. The TAB( ) function is only capable of moving left-to-right. In other words, you can't tab to column 30 and then back to column 15 in the same PRINT statement (back-tabbing is impossible). For example, consider the following:

    PRINT TAB(10); "Hello, world!"; TAB(25); "This is my first program."

This places the 'H' in "Hello, world!" in column 10 of the current output line. It also places the 'T' in "This is my first program." in column 25 of the same output line.

There is yet another function that allows you to control the format of output in a PRINT statement. It is the SPC( ) function. The SPC( ) function can be used to specify how many spaces (columns) to move over before the printing of an item begins. For example, consider the following:

    PRINT "Customer Name: "; SPC(10); cusName$

You can also use the LOCATE command, which is used to "jump" to a specified row and column of the output screen. For example, consider the following:

    LOCATE 15, 25
    PRINT "QBasic is easy"

The above code would begin printing on row 15 and column 25 of the output screen. Please note that the maximum number of rows is 25, and the maximum number of columns is 80.

If you want to print off a hard copy of some information, you can use the LPRINT statement. This would send the contents of the statement to the active printer. The LPRINT statement is used exactly the same as the PRINT statement.

You can also use the PRINT USING statement to control the complete output of a single line. A PRINT USING statement instructs the compiler to use a specified format, which is specified by the programmer. Special control characters are used to indicate the format. For example, consider the following:

    number = 21.2365
    PRINT USING "###.##"; number

The above code would produce the following output:


Each number symbol ( # ) in the statement represents a single digit. Since there are two symbols following the decimal point, the value of number is rounded to two decimal places. Using this type of format is useful for aligning numbers with decimal points. You can also place a dollar sign ( $ ) in front of a dollar amount by using the following code:

    PRINT USING "$###.##"; 4.23

This would produce the following output:

    $  4.23

Because there are three digits specified in the PRINT USING format and there is only 1 digit to the left of the decimal point in the actual value, there are two blank spaces placed between the dollar sign and the first digit. If you want the dollar sign to be placed in front of the first digit no matter what, you can use two dollar signs ( $$ ), which makes the dollar sign "float". For example, consider the following:

    PRINT USING "$$###.##"; 3.19

This would produce the following output:


When you want to use a particular PRINT USING format in more than one line of output, you should assign the format control characters to a string variable. You can then call this variable format whenever you need to use the PRINT USING statement. A backslash is another control character, which is used to format character strings. The string will be left justified in the output field. For example, consider the following:

    format$ = "\    \ $$###.##"
    PRINT USING format$; "computer", 983.4512

There are four spaces between the backslashes, but the maximum number of characters that can be output in this field is the number of spaces + 2 (4 + 2 = 6), so 6 is the maximum number of characters that can be printed in this field. If the string trying to be printed has more than 6 characters, then it is truncated or "cut" off to six characters. Therefore, the above code would produce the following output:

    comput $983.45

The following complete program demonstrates most of the topics covered in this section. Study this program and write one of your own before moving on to the next section.


REM PURPOSE: This program will calculate and display the percentage of workers
REM                     for WAREBIZ Programming Co. who earned bonuses in three
REM                     different departments of the company.


INPUT "Enter the total number of workers in the programming dpmt: ", progWorkers
INPUT "Enter the total number of workers in the research dpmt: ", researchWorkers
INPUT "Enter the total number of workers in the development dpmt: ", develWorkers

INPUT "Enter the number of workers who earned a bonus in the programming dpmt: ", progBonuses
INPUT "Enter the number of workers who earned a bonus in the research dpmt: ", researchBonuses
INPUT "Enter the number of workers who earned a bonus in the development dpmt: ", develBonuses

progPercent = (progWorkers / progBonuses) * 100
researchPercent = (researchWorkers / researchBonuses) * 100
develPercent = (develWorkers / progBonuses) * 100

Format$ = "  \            \        ##            ##          ###.#%"

PRINT TAB(22); "WAREBIZ Programming Co."
PRINT TAB(27); "Bonus Report"
PRINT "------------------------------------------------------------------"
PRINT TAB(2); "Department"; TAB(23); "Workers"; TAB(36); "Bonuses"; TAB(49); "Percentage"

PRINT "------------------------------------------------------------------"

PRINT USING Format$; "Programming"; progWorkers; progBonuses; progPercent
PRINT USING Format$; "Research"; researchWorkers; researchBonuses; researchPercent
PRINT USING Format$; "Development"; develWorkers; develBonuses; develPercent

PRINT "------------------------------------------------------------------"


We now know how to interact with the user and display results appropriately. We need to move on and talk about how to control the "flow" or execution of a program. Read on for more...

Move on to next set of topics: Section 3 - Controlling Execution With Control Structures

Back to Top