home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TURBOTUT.ZIP / MANUAL.EXE / CHAP10.TXT < prev    next >
Encoding:
Text File  |  1988-11-27  |  12.2 KB  |  322 lines

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