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


Chapter 6
Text Processing

6-1 Introduction

Thus far you have dealt mainly with numeric data values. In this chapter, you learn to use characters and strings to manipulate text data, paying special attention to input and output of characters and strings using the keyboard and the screen. These devices are treated as files; they are referred to as the standard INPUT file (the keyboard), and the standard OUTPUT file (the screen).

6-2 Tips on OUTPUT Statements

If you would like to display many lines of text, or display numeric results in separate lines, you can use as many WRITELN statements as the number of required lines. Another way to do this (one requiring less effort) is to use the ASCII control codes “13” (carriage return) and “10” (line feed) whenever a new line is required. You can then use one WRITE or WRITELN statement to print all of the results. With most microcomputer systems the carriage return/line feed pair is interpreted as the end-of-line mark. In the following example the control character “CHR(10)” is declared as a named constant “LF” (a common abbreviation for line feed), and the control character “CHR(13)” as “CR” (a common abbreviation for carriage return). The combination of the two characters “CR” and “LF” gives the same effect as pressing Enter.

{ -------------------- figure 6-1 -------------------- }
PROGRAM Display1(INPUT,OUTPUT);
CONST
 LF = CHR(10);
 CR = CHR(13);
VAR
 X, Y, Z:INTEGER;
BEGIN
 WRITE('Enter three integers: ');
 READLN(X, Y, Z);
 WRITELN('X=', X, CR, LF, 'Y=', Y, CR, LF, 'Z=', Z)
END.

Sample run:

Enter three integers: 11 22 33
X=11
Y=22
Z=33

If you tried this program using the “LF” only, you would get the following output:

X=11
  Y=22
    Z=33

Try it now using the “CR” only, and you will find that the last result overwrites the first two. The output will be only one line like this:

Z=33

6-3 Tips on INPUT Statements

When you use the input statements READ or READLN some pitfalls can occur during successive reads, especially with character input. For this reason it is important to understand how the input statements work with different types of data.

When a READ or a READLN statement is executed, values are stored in the standard INPUT file (the keyboard). The stored values are then read from this file and assigned to the variables specified in the input list. Each time you press the Enter key, an end-of-line mark is written to the INPUT file.

Using READLN for Numeric INPUT

Assume that your input contains the following numbers:

    123 45 678    <Enter>

You may imagine that the numbers are stored in the INPUT file as in the following figure:

1 2 3   4 5   6 7 8 *
file pointer ^

The end-of-line mark is shown at the last location and is indicated by the asterisk (*). At the first location, there is a little arrow (called the file pointer) pointing to the beginning of the file. Consider now that these values are read by the following statement:

    READLN(X, Y, Z);

After the first integer (123) is read and assigned to the variable “X,” the pointer moves to the space before the second numeric value (45). The second value is then read and assigned to the variable “Y,” and the pointer moves to the space before the third value. When the third value is read and assigned to “Z,” all of the variables will have been assigned values and the pointer moves past the end-of-line mark, where the work of the READLN statement ends. If you leave more than one space between numeric values, the extra spaces will be ignored and you will still get correct results.

Suppose now that you entered a fourth value by mistake:

    123 45 678 90 <Enter>

The last value (90) will be ignored by the program, as the pointer will move past the end-of-line mark after the three values are read, in order to be ready for a subsequent read.

1 2 3   4 5   6 7 8   9 0 *
file pointer ^


NOTE:  This feature of the READLN statement is inherited from the old days when data were read from punched cards (each card represents a line of data). The READLN was used to read only a specific number of items and eject to the next card.

You may also enter your numeric values separated by the Enter key, in which case each numeric value will be followed by the end-of-line mark like this:

1 2 3 * 4 5 * 6 7 8 *
file pointer ^


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.