|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
1-7 Reading from the Keyboard: READLN, READThe 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:
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
1-8 Formatting OutputYou have probably thought that scientific notation is not the best format for output, especially with business and money figures. Youre 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 SummaryIn this chapter you were introduced to the most important tools in Pascal programming.
|
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. |