home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PASCTUT1.ZIP / CHAP10.TXT < prev    next >
Encoding:
Text File  |  1986-07-14  |  12.5 KB  |  323 lines

  1.                     CHAPTER 10 - Standard Input/Output
  2.  
  3.  
  4.  
  5.             During  the  course of this tutorial we have been  using 
  6.  
  7.         the WRITE and WRITELN procedures to display data,  and it is 
  8.  
  9.         now time to discuss them fully.  Actually there is little to 
  10.  
  11.         be said that has not already been said,  but in order to get 
  12.  
  13.         all  of the data in one place they will be  redefined  here.  
  14.  
  15.         As  mentioned  earlier,  WRITE and WRITELN are not  actually 
  16.  
  17.         reserved words but are procedure calls.   They are therefore 
  18.  
  19.         merely  identifiers that could be changed but  there  should 
  20.  
  21.         never  be  a  reason to do so.   Lets get on  to  our  first 
  22.  
  23.         example program WRITELNX which has lots of output.
  24.  
  25.                           MANY OUTPUT STATEMENTS
  26.  
  27.             Pascal  has two output statements that are only slightly 
  28.  
  29.         different  in  the way they  work.   The  WRITELN  statement 
  30.  
  31.         outputs  all of the data specified within it,  then  returns 
  32.  
  33.         the  cursor  to the beginning of the next line.   The  WRITE 
  34.  
  35.         statement outputs all of the data specified within it,  then 
  36.  
  37.         leaves  the  cursor at the next character  where  additional 
  38.  
  39.         data  can be output.   The WRITE statement can therefore  be 
  40.  
  41.         used  to  output a line in bits and pieces  if  desired  for 
  42.  
  43.         programming convenience.  The first example program for this 
  44.  
  45.         chapter,  WRITELNX,  has  many  output statements  for  your 
  46.  
  47.         observation.   All  outputs are repeated so you can  observe 
  48.  
  49.         where the present field ends and the next starts.
  50.  
  51.             Observe the INTEGER output statements.  The first simply 
  52.  
  53.         directs the system to output "index" twice, and it does with 
  54.  
  55.         no  separating blanks.   The second statement says to output 
  56.  
  57.         "index" twice also,  but it instructs the system to put each 
  58.  
  59.         output  in  a field 15 characters wide with the  data  right 
  60.  
  61.         justified  in the field.   This makes the output  look  much 
  62.  
  63.         better.   This  illustrates  that you have complete  control 
  64.  
  65.         over the appearance of your output data.
  66.  
  67.             The  REAL output statements are similar to  the  integer 
  68.  
  69.         except  that when the data is put into a field 15 characters 
  70.  
  71.         wide,  it is still displayed in scientific format.  Adding a 
  72.  
  73.         second field descriptor tells the system how many digits you 
  74.  
  75.         want displayed after the decimal point.   The next few lines 
  76.  
  77.         illustrate the second field and its use.
  78.  
  79.             The BOOLEAN,  CHAR,  and STRING examples should be  self 
  80.  
  81.         explanatory.   Notice  that when the string is output,  even 
  82.  
  83.         though  the  string  has been defined as  a  maximum  of  10 
  84.  
  85.         characters,  it  has  been  assigned  a  string  of  only  8 
  86.  
  87.         characters, so only 8 characters are output.
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.                                   Page 46
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.                     CHAPTER 10 - Standard Input/Output
  105.  
  106.  
  107.                    NOW FOR SOME INPUT FROM THE KEYBOARD
  108.  
  109.             The  example  file READINT will illustrate reading  some 
  110.  
  111.         integer data from the keyboard.  A message is output with an 
  112.  
  113.         interesting fact that should be pointed out.  Anyplace where 
  114.  
  115.         Pascal  uses  a string variable or  constant,  it  uses  the 
  116.  
  117.         apostrophe  for  a  delimiter.    Thus,  anyplace  where  an 
  118.  
  119.         apostrophe is used in a string, it will end the string.  Two 
  120.  
  121.         apostrophes   in  a  row  will  be  construed  as  a  single 
  122.  
  123.         apostrophe  within  the string and will  not  terminate  the 
  124.  
  125.         string.  The term 'READ' within the string will therefore be 
  126.  
  127.         displayed as shown earlier in this sentence.
  128.  
  129.             The  variable "index" is used to loop five times through 
  130.  
  131.         a sequence of statements with one READ statement in it.  The 
  132.  
  133.         three integer variables are read in and stored with the  one 
  134.  
  135.         statement.   If  less than three are input in the statement, 
  136.  
  137.         only as many as are read in will be defined,  the rest  will 
  138.  
  139.         be unchanged.  Following completion of the first loop, there 
  140.  
  141.         is a second loop that will be executed 5 times with only one 
  142.  
  143.         minor  change,  the READ statement is replaced by the READLN 
  144.  
  145.         statement.   At this point it would be best run this program 
  146.  
  147.         trying several variations with input data.
  148.  
  149.             When  you run READINT,  it will request three  integers.  
  150.  
  151.         Reply with three small integers of your choice with as  many 
  152.  
  153.         blank  spaces  between  each as you desire,  followed  by  a 
  154.  
  155.         carriage  return.   The system will echo your three  numbers 
  156.  
  157.         back  out,  and request three more.   Respond with only  one 
  158.  
  159.         number  this time,  different from each of the first  three.  
  160.  
  161.         You  will  get  your new number followed  by  your  previous 
  162.  
  163.         second and third number indicating that you did not re-enter 
  164.  
  165.         the last two integer variables.  Enter all three again, this 
  166.  
  167.         time  including a negative number and observe the echo  once 
  168.  
  169.         again.
  170.  
  171.             Continue  entering numbers until the system outputs  the 
  172.  
  173.         message  indicating that it will now be using  the  'READLN' 
  174.  
  175.         for reading data.  At this point enter the same numbers that 
  176.  
  177.         you  did in the previous section and notice the  difference, 
  178.  
  179.         which is only very slight.   Each time you hit the enter key 
  180.  
  181.         to  cause  the  computer to process the data you  have  just 
  182.  
  183.         given it,  it will echo the carriage return to the  display, 
  184.  
  185.         and  the  "Thank you" message will be on a new  line.   When 
  186.  
  187.         entering data from the keyboard, the only difference in READ 
  188.  
  189.         and  READLN is whether or not the carriage return is  echoed 
  190.  
  191.         to the display following the data read operation.
  192.  
  193.             It should not be a surprise to you that after you  enter 
  194.  
  195.         the  data,  the data is stored within the program and can be 
  196.  
  197.  
  198.  
  199.  
  200.  
  201.                                   Page 47
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.                     CHAPTER 10 - Standard Input/Output
  212.  
  213.  
  214.         used anywhere that integer data is legal for use.  Thus, you 
  215.  
  216.         could read in an integer, and use the integer to control the 
  217.  
  218.         number of times through a loop, as a CASE selector, etc.
  219.  
  220.                         TIME TO CRASH THE COMPUTER
  221.  
  222.             Crashing the computer will not hurt a thing.   Rerun the 
  223.  
  224.         above  program and instead of entering integer  data,  enter 
  225.  
  226.         some real data with decimal points,  or even some  character 
  227.  
  228.         data.   The  computer  should display some kind  of  message 
  229.  
  230.         indicating that you have caused an I/O error (Input/Output), 
  231.  
  232.         and  most  implementations  of Pascal  will  probably  abort 
  233.  
  234.         operation  (that simply means to stop the program and return 
  235.  
  236.         control  to the operating system).   No harm has been  done, 
  237.  
  238.         simply start it again to enter more numbers or errors.
  239.  
  240.                            READING REAL NUMBERS
  241.  
  242.             The example program READREAL will illustrate how to read 
  243.  
  244.         real numbers into the computer.  It will read an integer and 
  245.  
  246.         three  real  numbers  each time through  the  loop.   It  is 
  247.  
  248.         perfectly fine to give the system a number without a decimal 
  249.  
  250.         point for a real number.   The computer will simply read  it 
  251.  
  252.         as  a decimal number with zeros after the decimal point  and 
  253.  
  254.         consider it as a real number internally. As you found out in 
  255.  
  256.         the last example program,  however, it is not permissible to 
  257.  
  258.         include  a  decimal  point in the data if  the  computer  is 
  259.  
  260.         looking  for  an integer variable.   Include some  character 
  261.  
  262.         data for a real number and crash the system in this  program 
  263.  
  264.         too.
  265.  
  266.                           READING CHARACTER DATA
  267.  
  268.             The  next example program,  READCHAR,  will read in  one 
  269.  
  270.         character each time through the loop and display it for you.  
  271.  
  272.         Try  entering more than one character and you will see  that 
  273.  
  274.         the  extra  characters will simply be ignored.   It  is  not 
  275.  
  276.         possible  to  crash this program because any  character  you 
  277.  
  278.         enter will be valid.
  279.  
  280.             The  next example,  READARRY,  will read in a string  of 
  281.  
  282.         characters  and display them for you.   Up to 10  characters 
  283.  
  284.         will be read, and if less than 10 are read, the rest will be 
  285.  
  286.         blank filled.   Try entering 10 characters,  then 4,  to see 
  287.  
  288.         that  the  residual  6  characters are  blanked  out  before 
  289.  
  290.         storing  and  printing.   Since the array is  fixed  at  ten 
  291.  
  292.         characters, ten characters are always printed out, including 
  293.  
  294.         trailing blanks.
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.                                   Page 48
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.                     CHAPTER 10 - Standard Input/Output
  312.  
  313.  
  314.             Finally READSTRG will also read up to 10 characters, but 
  315.  
  316.         since  a string is a dynamic length variable,  it will  only 
  317.  
  318.         print  out  the characters you input each time,  up  to  the 
  319.  
  320.         maximum  of 10 as defined in the VAR declaration.   It  will 
  321.  
  322.         display  trailing blanks if you type them in because  blanks 
  323.  
  324.         are valid characters.
  325.  
  326.                          BULLET PROOF PROGRAMMING
  327.  
  328.             It  can be frustrating to be running a program and  have 
  329.  
  330.         it  declare  an  I/O error and  terminate  operation  simply 
  331.  
  332.         because  you  have  entered  an  incorrect  character.   The 
  333.  
  334.         integer and real data inputs defined earlier in this chapter 
  335.  
  336.         are   fine   for  quick  little  programs  to  do   specific 
  337.  
  338.         calculations,  but  if you are writing a large  applications 
  339.  
  340.         program  it is better to use another technique.   Since  the 
  341.  
  342.         character  and string inputs cannot abort operation  of  the 
  343.  
  344.         program,  it  is best to use them to input the variable data 
  345.  
  346.         and  check  the  data  internally  under  your  own  program 
  347.  
  348.         control.  An error message can then be given to the operator 
  349.  
  350.         and  another opportunity granted to input the correct  data.  
  351.  
  352.         All  well  written  large  application  programs  use   this 
  353.  
  354.         technique.
  355.  
  356.                  HOW DO I PRINT SOMETHING ON THE PRINTER
  357.  
  358.             With all of the Pascal knowledge you now have, it is the 
  359.  
  360.         simplest thing in the world to get data to the printer.  The 
  361.  
  362.         example  file  PRINTOUT will show you graphically how to  do 
  363.  
  364.         it.   Every WRITE or WRITELN statement is required to have a 
  365.  
  366.         device identifier prior to the first output field.  If there 
  367.  
  368.         is  none,  it  is automatically defaulted  to  the  standard 
  369.  
  370.         output device, the display monitor.  The example program has 
  371.  
  372.         a  few  outputs  to the monitor with the  device  identifier 
  373.  
  374.         included,  namely "output".   This is only done to show  you 
  375.  
  376.         the  general form of the WRITE statements.   There are  also 
  377.  
  378.         many  statements in this program with the display identifier 
  379.  
  380.         "lst",  which is the standard name for the "list" device  or 
  381.  
  382.         the printer.  Compile and run this program with your printer 
  383.  
  384.         turned on for some printer output.
  385.  
  386.             Just  to supply you with a bit more  information,  every 
  387.  
  388.         READ  and READLN statement is also required to have a device 
  389.  
  390.         identifier  prior  to the first input  field.   As  you  may 
  391.  
  392.         suspect,  it  is  also  defaulted  to  "input"  if  none  is 
  393.  
  394.         specified, and the standard input device is the keyboard.
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.                                   Page 49
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.                     CHAPTER 10 - Standard Input/Output
  414.  
  415.  
  416.                            PROGRAMMING EXERCISE
  417.  
  418.         1.  Write a program containing a loop to read in a character 
  419.  
  420.             string  up to 60 characters long,  then print the string 
  421.  
  422.             on your printer. When you run the program, you will have 
  423.  
  424.             the  simplest word processing program in the  world.  Be 
  425.  
  426.             sure  to  include a test to end the loop,  such as  when 
  427.  
  428.             "END" is typed in.
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.                                   Page 50
  475.  
  476.