Chapter Twelve
Output Formatting, Part Three
Keyword: AND

For those of you who tried to write the program at the end of the last chapter with all of the bells and whistles, but found it nearly impossible, don't worry. It was quite a difficult program to write. It took us about 30 minutes to do it, and we have been programming in QUICKBASIC for over ten years! Enough whining, however. Let's get to the problem. Here is the basic essentials of the cash register program. It totals up the purchases, adds the tax, and then prints out the total.

```
LET SUBTTL = 0

NEXTITEM:

CLS

LOCATE 5, 40

PRINT "Running Total:";

PRINT USING "\$\$####.##"; SUBTTL

LOCATE 2, 2

PRINT "Enter Price:";

INPUT PRICE

IF PRICE = 0 THEN GOTO TOTALS

PRINT "Enter Quantity:";

INPUT QTY

IF QTY = 0 THEN GOTO NEXTITEM

LET SUBTTL = SUBTTL + (PRICE * QTY)

GOTO NEXTITEM

TOTALS:

CLS

PRINT

PRINT "Subtotal:";

PRINT USING "\$\$####.##"; SUBTTL

LET TAX = SUBTTL * .05

LET TAX = INT((100 * TAX) + .5) / 100

PRINT " Tax:";

PRINT USING "\$\$####.##"; TAX

PRINT

PRINT " TOTAL:";

TOTAL = SUBTTL + TAX

TOTAL = INT((100 * TOTAL) + .5) / 100

PRINT USING "\$\$####.##"; TOTAL

```

Notice how we rounded our tax and final total to the nearest penny (it's the green line). Keep that formula handy. It is a universal formula for rounding. It basically goes like so: Multiply your number by a desired amount so as to move the decimal point after the digit to be rounded. Then add 0.5 to that amount, and take the integer portion. Next, divide by the same amount that you multiplied by earlier. Simple, don't you think?

If you stopped here with your program, you really didn't need to round off your final answer. However, we went on with the program, and with the computer doing all of that addition and subtraction, some inaccuracies will creep in. If you have forgotten why we need to do this, go back and re-read chapter six. Anyway, here is the second part of the program. Adding this segment asks the cashier to enter the amount of cash used to pay, and then displays the change due:

```

INPUT CASH

CHANGE = CASH - SUBTTL - TAX

CHANGE = INT((100*CHANGE) +.5) / 100

PRINT "Change Due:";

PRINT USING "\$\$####.##";CHANGE

```

Now that wasn't too bad, was it? Next is the killer. Again, notice that we rounded to the nearest penny. That is because this next section has a separate program loop for each denomination of bill or coin to be returned. We have tested the program, and it worked well for us. However, with very large amounts of change due, there may be a sufficient number of "trips" through the loops to accumulate an error of greater than one cent. The advanced series will introduce a way to declare all of our variables as integers, and bypass that problem. It does create another problem, however. As integers, we would have to represent everything in pennies, and not dollars. Also, the computer stores integer numbers in a much more compact way than "regular" numbers, and as such, can only go to about 32,000. That would limit our total to \$320. The way around that is to use a different type of integer. Anyway, here is our "How to hand out the change" segment:

```
MORETENS:

IF CHANGE < 10 THEN GOTO FIVES

CHANGE = CHANGE - 10

TENS = TENS + 1

GOTO MORETENS

FIVES:

IF CHANGE < 5 THEN GOTO ONES

CHANGE = CHANGE - 5

FIVES = FIVES + 1

GOTO FIVES

ONES:

IF CHANGE<1 THEN GOTO QUARTERS

CHANGE = CHANGE - 1

ONES = ONES +1

GOTO ONES

QUARTERS:

IF CHANGE < .25 THEN GOTO DIMES

CHANGE = CHANGE - .25

QUARTERS = QUARTERS + 1

GOTO QUARTERS

DIMES:

IF CHANGE < .1 THEN GOTO NICKELS

CHANGE = CHANGE - .1

DIMES = DIMES + 1

GOTO DIMES

NICKELS:

IF CHANGE < .05 THEN GOTO PENNIES

CHANGE = CHANGE -.05

NICKELS = NICKELS + 1

GOTO NICKELS

PENNIES:

IF CHANGE < .005 THEN GOTO NOCHANGE

CHANGE = CHANGE -.01

PENNIES = PENNIES + 1

GOTO PENNIES

NOCHANGE:

PRINT "Return:"

IF TENS = 0 THEN GOTO NOTENS

PRINT TENS;"Tens"

NOTENS:

IF FIVES = 0 THEN GOTO NOFIVES

PRINT FIVES;"Fives"

NOFIVES:

IF ONES = 0 THEN GOTO NOONES

PRINT ONES;"Ones"

NOONES:

IF QUARTERS = 0 THEN GOTO NOQUARTERS

PRINT QUARTERS;"Quarters"

NOQUARTERS:

IF DIMES = 0 THEN GOTO NODIMES

PRINT DIMES;"Dimes"

NODIMES:

IF NICKELS = 0 THEN GOTO NONICKELS

PRINT NICKELS;"Nickels"

NONICKELS:

IF PENNIES = 0 THEN GOTO NOPENNIES

PRINT PENNIES;"Pennies"

NOPENNIES:

IF ABS(CASH-TOTAL)>.01 THEN GOTO CHANGE

PRINT "No Change"

CHANGE:

END```

Wow! Can you see why it took us a while? 117 lines (including blank ones)! If you want, you can download the cash register program.

Let's start to develop our quadratic equation program. This does not actually solve the equation, it merely displays it accurately. Going back to those good old high school days, you may remember that a quadratic equation looks like ax˛+bx+c=0. Right off the bat, we have a small problem. The computer does not allow us to show the exponent 2 for the first element (easily - it can be done). Therefore, we will use QBASIC's form of exponentiation, the "^" symbol. Let's develop the program.

The program will operate as such: The variables a, b, and c shall be input from the user, one at a time. The user shall input 0 for all three values to indicate that the program should terminate. After all three variables are received, the program will proceed to output the equation in the form "ax^2+bx+c=0", adjusting the addition signs to subtraction if negative values are entered. If a value of 1 is entered, only the x component should be displayed. if a value of -1 is entered, only the -x component should be displayed. If a value of 0 is entered, the particular term must not be displayed at all.

Sounds simple enough. Let's get our program started by clearing the screen and asking the user to enter the required values. So that the user has some idea as to what is going on, we will print the equation in the generic form at the top of the screen, and then ask for the values.

```

CLS

TOP:

PRINT "ax^2+bx+c=0"

PRINT

PRINT

INPUT A

INPUT B

INPUT C```

Now we come to our first problem. How can we check a, b, and c all at the same time? It's simple. We will use QBASIC's Boolean (logic) operators. Boolean functions are used in a very specialized form of mathematics, particularly in binary math. Since a computer is a very fast binary device, Boolean functions are very common. There are two quite common Boolean operators that are used in QBASIC. they are AND and OR, and they do just what they sound like they do. Instead of trying to explain it, we will show you our line that does it. Go ahead and type this in:

```

IF A = 0 AND B = 0 AND C = 0 THEN END```

It looks innocent enough. Simply stated, in an AND situation, all of the comparisons have to be true for the entire thing to be true. In an OR situation, at least one must be true (but more than one can be true) for the statement to be true. We will discuss these and other Boolean operators in a future chapter. Yes, there are a couple more. By the way, see how we can end the program in an IF...THEN statement? Anyway, let's worry about displaying the ax^2 element. For this, we need to take care of, and eliminate the special cases first. We'll start out with a=1. If so, we only print out "x^2". Here's how:

```

IF A <> 1 THEN GOTO ANOTONE

PRINT "x^2";```

Since that part was taken care of, we need to jump around the rest of the ax^2 part:

```

GOTO GETB```

Now we will handle a=-1:

```

ANOTONE:

IF A <> -1 THEN GOTO ANOTMINUS1

PRINT "-x^2";

GOTO GETB```

And the a=0. For this, we don't print anything, so we jump right to the beginning of our b section.

```

IF A = 0 THEN GOTO GETB```

Now all we have left is numbers to actually be printed. We'll use a PRINT USING function to handle it. In this case, if the number is positive, we don't need to add a plus sign, since it is the first element in the equation:

```

PRINT USING "###"; A;

PRINT " x^2";```

Now that we have the first element out of the way, let's work on the "b" part of the equation. It will be quite similar to the "a" part, but with a few exceptions. Let's start with a value of 1. Here, we need to print out a "+x" term, like this:

```

GETB:

IF B <> 1 THEN GOTO BNOT1

PRINT " +x";

GOTO GETC```

If b is -1, we need to print out a "-x" term, like so:

```

BNOT1:

IF B <> -1 THEN GOTO BNOTMINUS1

PRINT " -x";

GOTO GETC```

If b=0, we need to skip around the entire "b" code. We could have just as easily placed these lines up higher, speeding up the program slightly for b=0. Here's the skip:

```

BNOTMINUS1:

IF B = 0 THEN GOTO GETC```

At this point, all we have are actual coefficients that need to be printed. Again, we will use a PRINT USING command to print it, but we will use it slightly differently. In the first term, we did not need to print a plus sign. For this term, we do. If you wanted to get really tricky, you could write the program to print the plus sign only if the "a" coefficient were non-zero. We didn't feel like doing it that way, so it saved us about two or three program lines. Try to figure it out for yourself. Here is our last lines for the "b" term:

```

PRINT USING "+###"; B;

PRINT " x";```

We are going to stop here, since we covered a lot of ground in a short chapter. Meanwhile, see if you can figure out the rest of the program for yourself. If not, the next chapter will continue the program development. If so, see how closely your program is to ours. See you then!!

Introduced in This Chapter:
KeyWords: AND

Concepts: Specialized output formatting, comparing multiple equalities in a single IF...THEN statement.