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


Using READLN for Character Input

If you would like to enter your characters like this:

    AB     <Enter>
    CD     <Enter>

you have to get rid of the extra characters remaining in the file (the CR and the LF) by using the READLN statement.

In the following program two READLN statements are used, so you are able to enter two characters (or more) followed by Enter and start the next read with a clean buffer.

{ -------------------- figure 6-4 -------------------- }
PROGRAM CharReadln3(INPUT,OUTPUT);
CONST
 LF = CHR(10);
 CR  = CHR(13);
VAR
 C1, C2, C3, C4 :CHAR;
BEGIN
 WRITE('Enter two characters: ');
 READLN(C1, C2);
 WRITE('Enter two characters: ');
 READLN(C3, C4);
 WRITELN('Your inputs have been assigned to the variables as follows:', CR, LF,
     'C1= ', C1, CR, LF,
     'C2= ', C2, CR, LF,
     'C3= ', C3, CR, LF,
     'C4= ', C4);
 WRITELN('The corresponding ASCII codes are:', CR, LF,
     ORD(C1),' ', ORD(C2),' ', ORD(C3),' ',ORD(C4))
END.

Sample run:

Enter two characters: abcd      <Enter>
Enter two characters: efgh      <Enter>
Your inputs have been assigned to the variables as follows:
C1= a
C2= b
C3= e
C4= f
The corresponding ASCII codes are:
97 98 101 102

Input of Mixed Types

It is legal to use one READ (or READLN) statement for mixed numeric and character data, but this requires extra attention. It is better to use a separate READLN statement for each type, as in the following program. This way is less prone to data entry errors.

{ -------------------- figure 6-5 -------------------- }
PROGRAM CharNumRead(INPUT,OUTPUT);
CONST
 LF = CHR(10);
 CR  = CHR(13);
VAR
 A, B :CHAR;
 X, Y :INTEGER;
BEGIN
 WRITE('Enter two characters: ');
 READLN(A, B);
 WRITE('Enter two integers: ');
 READLN(X, Y);
 WRITELN('Your inputs have been assigned to the variables as follows:', CR, LF,
     'A= ', A, CR, LF,
     'B= ', B, CR, LF,
     'X= ', X, CR, LF,
     'Y= ', Y)
END.

Run 1:

Enter two characters: ABCD
Enter two integers: 3 4
Your inputs have been assigned to the variables as follows:
A= A
B= B
X= 3
Y= 4

As you can see in the output, the extra characters (C and D) were skipped after the first READLN. Remember, however, that the rules of character entry still apply; in other words, if you press Enter after the first letter, a CR will be assigned to the variable “B.” Here is the sample run:

Run 2:

Enter two characters: A      <Enter>
B                                  <Enter>
Enter two integers: 5 6
Your inputs have been assigned to the variables as follows:
A= A
B=               { B is assigned a CR }
X= 5
Y= 6

Example: Scrambling Letters

The following example is good practice both for handling characters and building loops. The program asks you to enter four characters, then it displays all of the possible combinations of those characters. If you are a BASIC programmer, you would have had to use a lot of GOTOs to achieve these results. In Pascal the program is better structured.

{ -------------------- figure 6-6 -------------------- }
PROGRAM Scrambling(INPUT,OUTPUT);
TYPE
 ScrambleArray = Array[1..4] OF CHAR;
VAR
 A            :ScrambleArray;
 I1, I2, I3, I4 :INTEGER;
BEGIN
 WRITE('Enter four letters: ');
 READ(A[1], A[2], A[3], A[4]);
 FOR I1 := 1 TO 4 DO
  BEGIN
   FOR I2 := 1 TO 4 DO
    BEGIN
     IF I2 <> I1 THEN
       FOR I3 := 1 TO 4 DO
        BEGIN
         IF I3 <> I1 THEN
           IF I3 <> I2 THEN
             BEGIN
              I4 := 10 - (I1 + I2 + I3);
              WRITELN(A[I1],' ',A[I2],' ',
                   A[I3],' ',A[I4]);
             END   { End of IF }
       END    { End of I3 loop }
    END    { End of I2 loop }
  END   { End of I1 loop }
END.

Sample run:

Enter four letters: ABCD
A B C D
A B D C
A C B D
A C D B
A D B C
A D C B
B A C D
B A D C
B C A D
B C D A
B D A C
B D C A
C A B D
C A D B
C B A D
C B D A
C D A B
C D B A
D A B C
D A C B
D B A C
D B C A
D C A B
D C B A

An array “A” of four elements (of the type CHAR) is used to hold the four characters, and three nested loops are used to build the different combinations of the elements. The algorithm is based on choosing four different indexes corresponding to the four different array elements.

Note that all of the BEGIN-END blocks (except the innermost one) are optional and are used only for clarity.

6-4 Reading a Line of Text: EOLN

The EOLN function is a boolean function used to detect the end of the line during reading of the INPUT file. The function is FALSE until the end-of-line mark is detected, then it becomes TRUE.

This function is useful when you do not know the number of characters to expect.

In order to read a line of text up to (but not including) the end-of-line mark, you can use a loop like this:

    WHILE NOT EOLN DO
     BEGIN
      READ(Ch);
      ...
     END;

The READ statement will continue to read characters until the end-of-line mark is detected, thus terminating the WHILE loop. Notice, however, that the end-of-line mark is still in the buffer and could be read by any subsequent READ statement, so before any subsequent read you have to clean the buffer with a READLN.

Example: Character Counter

The following program reads a line of text from the keyboard and displays the number of characters in the line. The program will continue to read the characters you type until you press Enter, at which time it displays the result.

{ -------------------- figure 6-7 ------------------- }
PROGRAM CharCounter1(INPUT,OUTPUT);
VAR
 Ch   :CHAR;
 Counter:INTEGER;
BEGIN
 Counter:= 0;
 WHILE NOT EOLN DO
  BEGIN
   READ(Ch);
   Counter:= Counter + 1
  END;
 WRITELN;
 WRITELN('Number of characters= ', Counter)
END.

Drill 6-3

Modify the previous program to count only the alphabetic characters in the text.


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.