home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PAS_TUT.ZIP / CHAP10.TXT < prev    next >
Encoding:
Text File  |  1991-02-04  |  11.5 KB  |  295 lines

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