|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
As long as the three variables have not yet been assigned values, the end-of-line marks between the values are treated as spaces and are thus ignored. The pointer moves from one end-of-line mark to another until all of the values have been read, then the pointer moves past the end of the next end-of-line mark, ending the READLN statement. Try the following program (which contains two READLN statements) using the values shown in the sample runs. {-------------------- figure 6-2 -------------------- } PROGRAM ReadLnNumbers(INPUT,OUTPUT); CONST CR = CHR(13); LF = CHR(10); VAR A, C, D, E:INTEGER; B :REAL; BEGIN WRITE('Enter A, B, C: '); { If you enter more than three values, only the first three will be read } READLN(A, B, C); { Now a subsequent READLN will start to read values after the end-of-line mark, ignoring any leftovers from the previous read } WRITE('Enter D, E: '); READLN(D, E); WRITELN('A=',A,', B=',B:0:2,', C=', C, CR, LF, 'D=', D,', E= ',E) END. Sample run: Enter A, B, C: 1 2 3 4 5 6 ----> Enter these values Enter D, E: 7 8 ----> Enter these values A=1, B=2.00, C=3 ----> The program response D=7, E= 8 Notice that the extra values (4, 5, 6) in the first input line were ignored completely and the second read started from the value 7, which follows the end-of-line mark.
Using READ for Numeric InputWith the READ statement the reading procedure is different, because after the READ statement is done, the file pointer does not move past the end-of-line mark, and so any subsequent READ will start from where the previous READ left off. Replace the READLN statements in the previous program with READ statements and try the following input: 1 2 3 4 5 6 7 <Enter> When you press Enter, the program will not pause at the second input statement because the input file contains sufficient numeric values for five variables. In this case, the program displays the following results: A=1, B=2.00, C=3 D=4, E=5
Using READ for Character InputWith character input, the input statements work in a different way. The READ statement reads successive characters from the keyboard file, including the end-of-line mark (which is actually two characters CR and LF), and assigns each character to the next variable in the input list. Consider the following input statement: READ(C1, C2, C3, C4); where C1, C2, C3, and C4 are variables of the type CHAR. If you enter the four characters that follow: ABCD they will all be read and assigned to the variables, thus:
Now consider the case of an input like this: A B C D The first four characters (including blank spaces) in this input will be assigned to the four variables and the rest ignored, giving the following result:
Run the following program and use the sample run values to see how things work. Notice that the output of the program gives you both the variables contents and the corresponding ASCII codes, which will help you to recognize any nonprintable character such as the space, the line-leed, or the carriage return. { -------------------- figure 6-3 -------------------- } PROGRAM CharRead1(INPUT,OUTPUT); CONST LF = CHR(10); CR = CHR(13); VAR C1, C2, C3, C4 :CHAR; BEGIN WRITE('Enter four characters: '); READ(C1, C2, 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. The following are sample runs of the program. Run 1: Enter four characters: A BCD Your inputs have been assigned to the variables as follows: C1= A C2= { blank space } C3= B C4= C The corresponding ASCII codes are: 65 32 66 67 The second variable was here assigned the ASCII code 32, which is the code for the blank space. Run 2: Enter four characters: ABCDEFG Your inputs are assigned to the variables as follows: C1= A C2= B C3= C C4= D The corresponding ASCII codes are: 65 66 67 68 In the second case, the first four characters are read and the rest are ignored. If there were a subsequent READ statement in the program, it would start at the letter E. The end-of-line mark is treated like any other nonnumeric character. For example, if you test the program using these inputs: AB <Enter> CD <Enter> the program will terminate after entering the first two characters and you will get an output like this: Run 3: C1= A C2= B C3= { CR } C4= { LF } The corresponding ASCII codes are: 65 66 13 10 The third and the fourth characters contain CR and LF respectively, because when you press Enter, you send two characters to the INPUT file, CR and LF. Notice that the CR appears as a blank space (actually, it returns the cursor to the beginning of the line), while the LF advances to a new line. The same thing will happen if you use two separate READ statements. To see this, replace the READ statement in the program by two READ statements: READ(C1, C2); READ(C3, C4); When you run the program now, you will notice that if you type the first two characters and press Enter, the program will be terminated and you get the same output as in RUN 3. Also, if you enter more characters than are required, only the first four will be read.
|
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. |