#### Example 2 choose New from the File menu and type in our next example:

PROGRAM stamps; {calculates the cost of stamps} VAR {by A Programmer} cost : REAL; number : INTEGER; total : REAL; BEGIN cost := 0.65; WRITELN('enter the number of stamps'); READLN(number); total := number * cost; WRITELN('you ordered ', number, ' stamps'); WRITELN('the total cost is ', total) END.

save the program as stamps.pas compile and run the program It should be obvious that the result is not what you might have expected. To explain why that is, let's look more closely at what's happening. Firstly, the program uses three numbers, or variables, called cost, number and total. The one called number is an integer (a whole number) because it doesn't make much sense to have, say, 0.7 of a stamp! The other numbers, however - called cost and total - are not, of course, always going to be whole numbers and so they are described as type REAL which means they can have a decimal part. That's exactly what has happened and it's just that the answer is not written in a way that is clear to us. We can solve it though... change the second last line to this: WRITELN('the total cost is ', total :5:2); This is just a bit of formatting so that, when the number total is printed, 5 spaces will be allowed for it and there will be 2 numbers after the decimal point. Easy, see... The program you've just done is different from the first one in that the user had to enter information when the program is run. In other words, it is interactive as, indeed is most of the software that you use. An improvement that we might consider making is to allow the user to say what the price of each stamp is, rather than assume that it's 65c. How could you make the necessary changes? Here's a clue: you'll need to replace the line cost := 0.65; with two lines that let the user type in the cost of a stamp. Here's another clue: the two lines will be similar to the part of the program that asks the user to type in the number of stamps... See your teacher (Mike Hardy) if you're not sure how to do this. make the changes to the program compile and run the program check that it works
© 2001 by Mike Hardy