Chapter Eleven
Output Formatting, Part Two
Keyword: PRINT USING


As usual, here are the answers to the problems at the end of the previous chapter. This is the program that we came up with for the coin flipping problem. Of course, it won't be exactly like yours, but it will probably be similar. To find out if yours is correct, run both yours and ours, and see if they operate in a similar manner. The location of the displayed output on the screen may be different, too. Anyway, here is our program:

Download the Coin-Flipping Program Now!


CLS

RANDOMIZE TIMER / 3

LOCATE 5, 19

PRINT "HEADS"

LOCATE 5, 39

PRINT "TAILS"

LOCATE 6, 20

PRINT HEADS

LOCATE 6, 40

PRINT TAILS

FOR COIN = 1 TO 2500

    LET FLIP = INT(2 * RND) + 1

    IF FLIP = 1 THEN GOTO ISHEADS

    LET TAILS = TAILS + 1

    LOCATE 6, 40

    PRINT TAILS

    GOTO NEXTFLIP

ISHEADS:

    LET HEADS = HEADS + 1

    LOCATE 6, 20

    PRINT HEADS

NEXTFLIP:

NEXT COIN

END

When we came up with the problem, we hadn't yet developed the program for the solution, and discovered that it would have saved four lines to not print out the zeros initially. Oh, well. That's life.

Here is the program to draw the diagonal line of asterisks:


CLS

FOR STAR=1 TO 22

    LOCATE STAR, STAR

    PRINT"*"

NEXT STAR

END

To modify this program to get the next one, simply change line 3 to read:


    LOCATE STAR, 2 * STAR

In this chapter, we will reintroduce the PRINT command, but this time it has a powerful extension to it. In fact, it does so much that we won't even discuss all of its features here. That's because some of them have to do with string variables, which we will introduce later on. Let's get right into it. Here is an example program for you to type in:



CLS

FOR NUM = 1 TO 20000

    LOCATE 5,10

    PRINT NUM

NEXT NUM

END

Now run the program, and notice how it behaves. If you didn't catch it, run it a few more times. Do you notice that as the number grows larger (has more digits) that it "grows" to the right? This means that the numbers are "left justified". Run it again, and notice especially at the start that the fastest changing number moves to the right by one position as the number grows larger. Now change line 4 to read:



    PRINT USING "#####";NUM

Now run the program a few more times. Notice that now the number "grows" to the left? In other words, the ones position does not shift over as the number becomes larger. This is called "right justified", and is useful for columns of numbers, because the digits will line up.

What do those number signs do, and why are there five of them in the PRINT USING command? The number sign represents a digit in the output format. In other words, the USING part of the PRINT command tells the computer just how to print out our numeric variable. Since we have 5 #'s, we are telling the computer to reserve 5 spaces to print out digits. If we needed to print a larger number, we would need to add another number sign. What happens if there are more digits than number signs? Let's find out. Change line 4 to:



    PRINT USING "####";NUM

Now run the program. You won't notice a difference until the number 10000 is printed. What do you get? That's right, it prints out %10000. That percent sign is QBASIC's way of showing us that we have a formatting error. It does not stop the program in its tracks, though. To fix that little formatting problem, simply add another number sign or two.

It sounds simple so far, but let's try something else. Change the program to (Changes are in green):


CLS

FOR NUM = 1 TO 2000 STEP .1

    LOCATE 5, 10

    PRINT USING "####"; NUM


NEXT NUM

END

Now run the program. Does it do what you expected? NO! We expected it to count from 1 to 2000, incrementing by 0.1 each time, but all we get are whole numbers! Why? We did not specify a decimal point in our format, so QBASIC assumes that it goes at the end. How do we fix that? Change line 4 to:



    PRINT USING "####.#";NUM

Now run the program again. Much better, wouldn't you say? What was happening to the number when the decimal point was being cut off? Let's find out. Type this in the immediate window:



PRINT USING "#";1.4

Do you see what the answer is? The computer spits back a "1". I guess we could expect that. Now try this:



PRINT USING "#";1.5

AH-HA!! Now the computer gave us a "2". This thing is pretty smart! It knows how to round off the answers! Let's try something else. Change the program to:


CLS

FOR NUM = 900 TO -900 STEP -1

    LOCATE 5,10

    PRINT USING "###"; NUM

NEXT NUM

END

and run the program. Do you notice our little formatting error symbol? Why is that? It's because we specified 3 digit positions. It works fine for the positive numbers, but when we get to the negative numbers, we must specify a space for the minus sign. Change line 4 to:



    PRINT USING "####";NUM

Now RUN the program again. Ah, that's better! But suppose we are doing some kind of engineering work, and we would like the sign to be printed even for a positive answer. What do we do then? Well, one of the specifiers for our format is the plus sign, +. To see it in action, change line 4 to:



    PRINT USING "+###";NUM

Now run it and watch closely. Did you notice that the program printed a plus sign for the positive numbers, and a minus sign for the negative numbers? If you missed them, run the program a few more times. The numbers fly by fast, so watch closely. Change the STEP size to -0.1 or -0.01 to slow it down if you have to.

Let's switch to accounting type problems. Suppose we have some huge numbers we want to print out. Let's start a new program to help us with these next few examples:



CLS

LET NUM = 1

MORE:

LOCATE 5, 10

PRINT USING "#######"; NUM

LET NUM = NUM * 1.01

IF NUM < 10000000 THEN GOTO MORE

END

Go ahead and run the program. It goes kind of fast. Notice our final value. Is it nine hundred thousand something, or nine million something? How do you clean it up to read it easier? Most people write extremely large number by putting commas in them. Did you know that QBASIC can do that, too? Here's how to specify it. Change line 4 to:



PRINT USING "#######,";NUM

Now run the program again. Oh,look! We get that format overflow signal again! Why? It's because the commas take up a couple of character positions! Once again, we just need to add a couple more #'s to fix it:



PRINT USING "#########,";NUM

Now run it again. Much better. Another fix would be to write the program line this way:



PRINT USING "#,###,###";NUM

Next problem. Suppose our variable NUM represents a dollar amount. Can we put a dollar sign in front of the number? Yes. Let's change line 4 once again:



PRINT USING "$#########";NUM

Run it again. Now you have a dollar sign off to the left of the number. That's nice, but what if we want it right next to the number? QBASIC has a provision for that. To make the dollar sign "float", you simply place two of them at the front of the format, like this:



PRINT USING "$$#########";NUM

Now run it again. Looks a little better, but can we combine two things together? We like the floating dollar sign, but it would also help to have the commas in there, too. Here is our next line:



PRINT USING "$$########,";NUM

Go ahead and run it. One more problem with this format. When you specify dollars, you usually will want to display some cents to go along with it. To do that, all you need to do is specify two digit positions after the decimal point, like such:



PRINT USING "$#########,.##";NUM

Run it again. Do you notice something interesting about that final value? Even though the cents part is zero, the computer still prints out the zero digits. When you use PRINT USING, any specified digits after the decimal point are always printed, even if they are zero. Type this in the immediate window:



PRINT USING "#.####";0

Notice how all of the digits are printed? It makes for a more uniform look.

Back to the Program Window. Now let's say you are writing a program that will print out on pre-printed checks. To prevent tampering, many programs will fill in unused positions to the left of the number with asterisks. How do we do that in QBASIC? Like this:



    PRINT USING "**#########,.##";NUM

Now run it and see what happens. Neat, huh? But suppose your check doesn't have a dollar sign printed on it? We want to have our asterisk filler, followed by our floating dollar sign, followed by our number, complete with comma seperators. You guessed it, here is our new line:



    PRINT USING "**$#########,.##";NUM

Run it one last time. Neat, huh?

Well, that's about all we have room for in this chapter. In the next chapter, we will put some of these PRINT USING formats to use, displaying a quadratic equation. But for next time, here is a program for you to try. It is a little more difficult, so it will be the only one we will give you. If you figure it out, make up some of your own.

Write a "cash register" program. Have the user input the price of a product, and the quantity of that product. Display a running total at all times. When the user enters a price of zero, that will indicate that there are no more items. When all items are entered, display the sub-total, calculate and display a 5% tax, and then display the total. If you feel adventurous, have the user enter the amount of cash tendered, and display the amount of change to be returned. For the really brave, have the computer calculate the number of $10, $5, $1 bills and quarters, dimes, nickels, and pennies to be returned as change.

See you next time!!

Introduced in this chapter:
Keywords: PRINT USING

Concepts: Formatting numerical output for easier reading and "accounting" type output.


Previous Chapter Next Chapter