Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


1-7 Reading from the Keyboard: READLN, READ

The previous program is used to calculate the perimeter for a given radius, hardcoded in the program. A more useful program would accept the radius from the user, do the calculations, then display the result. You can use either READLN or READ to make the program pause and wait for user input. The READLN statement is used to read the value of one or more variables. It takes the general form:

READLN(variable-list);

To read the value of a variable “x” from the keyboard, you can use the statement:

    READLN(x);

To read the values of three variables x, y, and z use the statement:

    READLN(x, y, z);

When you enter the values of more than one variable (such as x, y, and z), they should be separated by one or more blanks or by pressing the Enter key.

Replace the assignment statement in the previous program with a READLN statement as follows:

    READLN(Radius);

If you try the program now, it will pause until you type a number and press Enter; it then resumes execution and displays the results. Unfortunately, you cannot use the READLN statement to display a user prompt when the program is waiting for input. This must be done using a WRITE (or WRITELN) statement such as:

    WRITE('Please enter the radius:');

Here is the program in its new shape:

{ --------------------------- figure 1-11 --------------------------- }
PROGRAM KeyboardInput(OUTPUT);
{ Constant Declarations }
CONST
 Pi = 3.14159;
{ Variable Declarations }
VAR
 Perimeter, Radius :REAL;
 RoundedPerimeter, TruncatedPerimeter :INTEGER;
{ Program block }
BEGIN
 WRITE('Please enter the radius:');
 READLN(Radius);
 Perimeter:= 2*Pi*Radius;
 RoundedPerimeter:= ROUND(Perimeter);
 TruncatedPerimeter:= TRUNC(Perimeter);
 WRITELN('Perimeter=', Perimeter);
 WRITELN('Perimeter (rounded)=', RoundedPerimeter);
 WRITELN('Perimeter (truncated)=', TruncatedPerimeter)
END.

A sample run of the program gives the following:

Please enter the radius:4.9  ----> Type the number and press Enter
Perimeter= 3.0787582000E+01
Perimeter (rounded)=31
Perimeter (truncated)=30


NOTE:  At this stage you can use either READ or READLN for keyboard input as the difference between them is not noticeable in our applications so far.

1-8 Formatting Output

You have probably thought that scientific notation is not the best format for output, especially with business and money figures. You’re right. Scientific notation is useful only with very large or very small numbers, where the power of ten represents an order of magnitude of the number. Whenever you want to see your results in fixed-point notation, use the format descriptors as in this example:

    WRITELN(Wages:6:2);

The format “:6:2” determines a field width of 6 positions, including 2 decimal places. So, if the value of the variable “wages” is 45.5 it will be displayed as:

    B45.50

where the letter “B” refers to a blank space. If the output digits are less than the field width, which is the case in this example, the result will be right shifted. If the number is larger than the field width, then the field will be automatically enlarged and the entire number printed.

You can add a character (such as the dollar sign) to the left of the number as follows:

    WRITELN('$',Wages:6:2);

This will produce the output:

    $ 45.50

By using a smaller field width, you can have the number shifted to the left and the dollar sign attached to the first significant digit:

    WRITELN('$',Wages:0:2);

This will produce:

    $45.50

You can format any type of data using the same method. The only difference is that with integers or strings you specify the width field without decimal places.

In the following program different types of data are formatted to fit into specific fields, as shown in the output.

{ ---------------------------  figure 1-12 --------------------------- }
PROGRAM Format(OUTPUT);
{ Variable Declarations }
VAR
 a :INTEGER;
 b :REAL;
{ Program Block }
BEGIN
 b:= 1.2e+02;
 a:= 320;
 WRITELN('I am a text string starting from position 1.');
 WRITELN('I am now shifted to the right end of the field.':50);
 WRITELN('I am an unformatted integer:', a);
 WRITELN('I am an integer written in a field 6 characters wide:', a:6);
 WRITELN('I am a money amount written in 8 positions:$',b:8:2);
 WRITELN('I am a money amount shifted to the left:$',b:0:2)
END.

The output is:

I am a text string starting from position 1.
  I am now shifted to the right end of the field.
I am an unformatted integer:320
I am an integer written in a field 6 characters wide: 320
I am a money amount written in 8 positions:$ 120.00
I am a money amount shifted to the left:$120.00

If you display the numeric variables alone (without text), they will appear as follows:

320
 320
$ 120.00
$120.00

Drill 1-5

Write a program to calculate employee wages according to the formula:

               Wages:= HoursWorked * PayRate;

Accept the “HoursWorked” and the “PayRate” from the keyboard and display the “Wages” in fixed-point notation preceded by a dollar sign.

Summary

In this chapter you were introduced to the most important tools in Pascal programming.

1.  You are now familiar with the Pascal program structure:
  The program heading
  The Declaration part
  The CONST section
  The VAR section
  The program main body between BEGIN and END
2.  You know two important data types, INTEGER and REAL, and how to express and evaluate arithmetic expressions using both types.
3.  You know the arithmetic operators in Pascal, their properties, and their precedence.
+ - * / DIV MOD
4.  You know how to declare variables of both types, how to name them using identifiers, how to store values in them whether by assignment (:=) or by entering values from the keyboard, and how to display their values on the screen.
5.  You know as well how to declare named constants and use them in the program.
6.  During you first tour of Pascal, you learned the following output statements to display both variables and numeric or string literal constants:
WRITELN
WRITE

Also, you learned the following input statements to read variable values from the keyboard:
READLN
READ
7.  Finally, you learned how to format your numeric or string output to have the results in the desired form.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.