home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / PASTUT24.ZIP / PTUTRTXT.ZIP / CHAP10.TXT < prev    next >
Encoding:
Text File  |  1989-12-01  |  12.5 KB  |  296 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                    Chapter 10
  6.                                         STANDARD INPUT/OUTPUT
  7.  
  8.  
  9. WE'VE USED THIS ALREADY
  10. ____________________________________________________________
  11.  
  12. During the course of this tutorial we have been using the
  13. Write and Writeln procedures to display data, and it is now
  14. time to discuss them fully.  Actually there is little to be
  15. said about them that has not already been said, but in order
  16. to get all of the data in one place, they will be redefined
  17. here.
  18.  
  19. As mentioned earlier, Write and Writeln      ================
  20. are not actually reserved words but are        WRITELNX.PAS
  21. procedure calls.  They are therefore         ================
  22. merely identifiers that could be changed,
  23. but there should never be a reason to do
  24. so.  Let's get on to our first example program WRITELNX.PAS
  25. which has lots of output.
  26.  
  27.  
  28.  
  29. MANY OUTPUT STATEMENTS
  30. ____________________________________________________________
  31.  
  32. Pascal has two output statements with only slight differences
  33. in the way they work.  The Writeln statement outputs all of
  34. the data specified within it, then returns the cursor to the
  35. beginning of the next line.  The Write statement outputs all
  36. of the data specified within it, then leaves the cursor at the
  37. next character where additional data can be output.  The Write
  38. statement can therefore be used to output a line in bits and
  39. pieces if desired for programming convenience.  The first
  40. example program for this chapter, WRITELNX.PAS, has many
  41. output statements for your observation.  All outputs are
  42. repeated so you can observe where the present field ends and
  43. the next starts.
  44.  
  45. Observe the two integer output statements in lines 13 and 14. 
  46. The first simply directs the system to output Index twice, and
  47. it outputs the value with no separating blanks.  The second
  48. statement says to output Index twice also, but it instructs
  49. the system to put each output in a field 15 characters wide
  50. with the data right justified in the field.  This makes the
  51. output look much better.  This illustrates that you have
  52. complete control over the appearance of your output data.
  53.  
  54. The real output statements in lines 19 and 20 are similar to
  55. the integer except that when the data is put into a field 15
  56. characters wide, it is still displayed in scientific format. 
  57. Adding a second field descriptor as illustrated in lines 21
  58.  
  59.                                                     Page 10-1
  60.  
  61.                                         Standard Input/Output
  62.  
  63. through 23, tells the system how many digits you want
  64. displayed after the decimal point.
  65.  
  66. The boolean, char, and string examples should be self
  67. explanatory.  Notice that when the string is output, even
  68. though the string has been defined as a maximum of 10
  69. characters, it has been assigned a string of only 8
  70. characters, so only 8 characters are output.  Compile and run
  71. this program and observe the results.
  72.  
  73. If you are using TURBO Pascal version 4.0 or 5.0, the added
  74. data types described in chapter 3 of this tutorial are output
  75. in the same manner as those illustrated in the program
  76. WRITELNX.PAS.
  77.  
  78.  
  79.  
  80. NOW FOR SOME INPUT FROM THE KEYBOARD
  81. ____________________________________________________________
  82.  
  83. The example file READINT.PAS will           =================
  84. illustrate reading some integer data from      READINT.PAS
  85. the keyboard.  A message is output in line  =================
  86. 8 with an interesting fact that should be
  87. pointed out.  Anyplace where Pascal uses a
  88. string variable or constant, it uses the apostrophe for a
  89. delimiter.  Therefore, anyplace where an apostrophe is used
  90. in a string, it will end the string.  Two apostrophes in a row
  91. will be construed as a single apostrophe within the string and
  92. will not terminate the string.  The term 'Read' within the
  93. string will therefore be displayed as shown earlier in this
  94. sentence.
  95.  
  96. The variable Index is used to loop five times through a
  97. sequence of statements with one Read statement in it.  The
  98. three integer values are read in and stored in their
  99. respective variables with the one statement.  If less than
  100. three are entered at the keyboard, only as many as are read
  101. in will be defined, the rest will be unchanged.  Following
  102. completion of the first loop, there is a second loop in lines
  103. 19 through 25 that will be executed 5 times with only one
  104. minor change, the Read statement is replaced by the Readln
  105. statement.  At this point it would be best run this program
  106. trying several variations with input data.
  107.  
  108. When you run READINT.PAS, it will request three integers. 
  109. Reply with three small integers of your choice with as many
  110. blank spaces between each as you desire, followed by a
  111. carriage return.  The system will echo your three numbers back
  112. out, and request three more.  Respond with only one number
  113. this time, different from each of the first three, and a
  114. carriage return.  You will get your new number followed by
  115. your previous second and third number indicating that you did
  116. not re-enter the last two integer variables.  Enter three more
  117.  
  118.                                                     Page 10-2
  119.  
  120.                                         Standard Input/Output
  121.  
  122. numbers, this time including a negative number and observe the
  123. echo once again.
  124.  
  125. Continue entering numbers until the system outputs the message
  126. indicating that it will now be using the Readln for reading
  127. data.  At this point enter the same numbers that you did in
  128. the previous section and notice the difference, which is only
  129. very slight.  Each time you hit the enter key to cause the
  130. computer to process the data you have just given it, it will
  131. echo the carriage return to the display, and the "Thank you"
  132. message will be on a new line.  When entering data from the
  133. keyboard, the only difference in Read and Readln is whether
  134. or not the carriage return is echoed to the display following
  135. the data read operation.
  136.  
  137. It should not be a surprise to you that after you enter the
  138. data, the data is stored within the program and can be used
  139. anywhere that integer data is legal for use.  Thus, you could
  140. read in an integer, and use the integer to control the number
  141. of times through a loop, as a case selector, etc.
  142.  
  143.  
  144.  
  145. TIME TO CRASH THE COMPUTER
  146. ____________________________________________________________
  147.  
  148. Crashing the computer will not hurt a thing.  Rerun the above
  149. program and instead of entering integer data, enter some real
  150. data with decimal points, or even some character data.  The
  151. computer should display some kind of message indicating that
  152. you have caused an I/O error (Input/Output), and TURBO Pascal
  153. will abort operation (that simply means to stop the program
  154. and return control to the operating system).  No harm has been
  155. done, simply start it again to enter more numbers or errors.
  156.  
  157.  
  158.  
  159. READING REAL NUMBERS
  160. ____________________________________________________________
  161.  
  162. The example program READREAL.PAS will        ================
  163. illustrate how to read real numbers into       READREAL.PAS
  164. the computer.  It will read an integer and   ================
  165. three real numbers each time through the
  166. loop.  It is perfectly fine to give the
  167. system a number without a decimal point for a real number. 
  168. The computer will simply read it as a decimal number with
  169. zeros after the decimal point and consider it as a real number
  170. internally. As you found out in the last example program,
  171. however, it is not permissible to include a decimal point in
  172. the data if the computer is looking for an integer variable. 
  173. Include some character data for a real number and crash the
  174. system in this program too.
  175.  
  176.  
  177.                                                     Page 10-3
  178.  
  179.                                         Standard Input/Output
  180.  
  181. READING CHARACTER DATA
  182. ____________________________________________________________
  183.  
  184. The next example program, READCHAR.PAS,      ================
  185. will read in one character each time           READCHAR.PAS
  186. through the loop and display it for you.     ================
  187. Try entering more than one character and
  188. you will see that the extra characters
  189. will simply be ignored.  It is not possible to crash this
  190. program because any character you enter will be valid.
  191.  
  192. The next example, READARRY.PAS, will read    ================
  193. in a string of characters and display them     READARRY.PAS
  194. for you if you are using TURBO Pascal 3.0.   ================
  195. Neither TURBO Pascal 4.0 nor 5.x permits
  196. reading into an array but does allow
  197. reading into the individual elements of the array one element
  198. at a time.  This program does not work with TURBO Pascal 4.0
  199. or 5.x so you should go directly to the next program,
  200. READSTRG.PAS, if you are using either of the two newer
  201. versions. 
  202.  
  203. Continuing our discussion of READARRY.PAS, up to 10 characters
  204. will be read, and if less than 10 are read, the rest will be
  205. blank filled.  Try entering 10 characters, then 4, to see that
  206. the residual 6 characters are blanked out before storing and
  207. printing.  Since the array is fixed at ten characters, ten
  208. characters are always printed out, including trailing blanks.
  209.  
  210. Finally, READSTRG.PAS will also read up to   ================
  211. 10 characters, but since a string is a         READSTRG.PAS
  212. dynamic length variable, it will only        ================
  213. print out the characters you input each
  214. time, up to the maximum of 10 as defined
  215. in the var declaration.  It will display trailing blanks if
  216. you type them in because blanks are valid characters.
  217.  
  218.  
  219. BULLET PROOF PROGRAMMING
  220. ____________________________________________________________
  221.  
  222. It can be frustrating to be running a program and have it
  223. declare an I/O error and terminate operation simply because
  224. you have entered an incorrect character.  The integer and real
  225. data inputs defined earlier in this chapter are fine for quick
  226. little programs to do specific calculations, but if you are
  227. writing a large applications program it is better to use
  228. another technique.  Since the character and string inputs
  229. cannot abort operation of the program, it is best to use them
  230. to input the variable data and check the data internally under
  231. your own program control.  An error message can then be given
  232. to the operator and another opportunity granted to input the
  233. correct data.  All well written large application programs use
  234. this technique.
  235.  
  236.                                                     Page 10-4
  237.  
  238.                                         Standard Input/Output
  239.  
  240.  
  241. HOW DO I PRINT SOMETHING ON THE PRINTER
  242. ____________________________________________________________
  243.  
  244. With all of the Pascal knowledge you now     ================
  245. have, it is the simplest thing in the          PRINTOUT.PAS
  246. world to get data to the printer.  The       ================
  247. example file PRINTOUT.PAS will show you
  248. graphically how to do it.  Every Write or
  249. Writeln statement is required to have a device identifier
  250. prior to the first output field.  If there is none, it is
  251. automatically defaulted to the standard output device, the
  252. display monitor.  The example program has a few outputs to the
  253. monitor in lines 9 and 10 with the device identifier included,
  254. namely Output.  This is only done to show you the general form
  255. of the Write statements, but if you desire, you can add the
  256. standard device identifier to every monitor output.
  257.  
  258. There are many statements in this program with the device
  259. identifier Lst, which is the standard name for the list device
  260. or the printer.  It should be obvious to you that the first
  261. field is the device selector which is used to direct the
  262. output to the desired device.
  263.  
  264. Compile and run this program with your printer turned on for
  265. some printer output.  If you are using TURBO Pascal 3.0, you
  266. will have to comment out line 4 since it will not be
  267. understood by your compiler.  It is required with version 4.0
  268. or 5.x to tell the system where to find the definition of the
  269. output device named Lst.
  270.  
  271. Just to supply you with a bit more information, every Read and
  272. Readln statement is also required to have a device identifier
  273. prior to the first input field.  As you may suspect, it is
  274. also defaulted to Input if none is specified, and the standard
  275. input device is the keyboard.
  276.  
  277.  
  278. PROGRAMMING EXERCISE
  279. ____________________________________________________________
  280.  
  281. 1.   Write a program containing a loop to read in a character
  282.      string up to 60 characters long, then print the string
  283.      on your printer. When you run the program, you will have
  284.      the simplest word processing program in the world. Be
  285.      sure to include a test to end the loop, such as when
  286.      "END" is typed in.
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.                                                     Page 10-5
  296.