


                                                       Chapter 10
                                            STANDARD INPUT/OUTPUT


WE'VE USED THIS ALREADY
_________________________________________________________________

During the course of this tutorial we have been using the Write and
Writeln procedures to display data, and it is now time to discuss
them fully.  Actually there is little to be said about them that
has not already been said, but in order to get all of the data in
one place, they will be redefined here.

As mentioned earlier, Write and Writeln are not  ================
actually reserved words but are procedure calls.   WRITELNX.PAS
They are therefore merely identifiers that could ================
be changed, but there should never be a reason
to do so.  Let's get on to our first example
program WRITELNX.PAS which has lots of output.



MANY OUTPUT STATEMENTS
_________________________________________________________________

Pascal has two output statements with only slight differences in
the way they work.  The Writeln statement outputs all of the data
specified within it, then returns the cursor to the beginning of
the next line.  The Write statement outputs all of the data
specified within it, then leaves the cursor at the next character
where additional data can be output.  The Write statement can
therefore be used to output a line in bits and pieces if desired
for programming convenience.  The first example program for this
chapter, WRITELNX.PAS, has many output statements for your
observation.  All outputs are repeated so you can observe where the
present field ends and the next starts.

Observe the two integer output statements in lines 13 and 14.  The
first simply directs the system to output Index twice, and it
outputs the value with no separating blanks.  The second statement
says to output Index twice also, but it instructs the system to put
each output in a field 15 characters wide with the data right
justified in the field.  This makes the output look much better. 
This illustrates that you have complete control over the appearance
of your output data.

The real output statements in lines 19 and 20 are similar to the
integer except that when the data is put into a field 15 characters
wide, it is still displayed in scientific format.  Adding a second
field descriptor as illustrated in lines 21 through 23, tells the
system how many digits you want displayed after the decimal point.



                                                        Page 10-1

                               Chapter 10 - Standard Input/Output

The boolean, char, and string examples should be self explanatory. 
Notice that when the string is output, even though the string has
been defined as a maximum of 10 characters, it has been assigned
a string of only 8 characters, so only 8 characters are output. 
Compile and run this program and observe the results.

The new data types in TURBO Pascal which were described in chapter
3 of this tutorial are output in the same manner as those
illustrated in the program WRITELNX.PAS.



NOW FOR SOME INPUT FROM THE KEYBOARD
_________________________________________________________________

The example file READINT.PAS will illustrate      ===============
reading some integer data from the keyboard.  A     READINT.PAS
message is output by line 8 with an interesting   ===============
fact that should be pointed out.  Anyplace where
Pascal uses a string variable or constant, it
uses the apostrophe for a delimiter.  Therefore, anyplace where an
apostrophe is used in a string, it will end the string.  Two
apostrophes in a row will be construed as a single apostrophe
within the string and will not terminate the string.  The term
'Read' within the string will therefore be displayed as shown
earlier in this sentence.

The variable Index is used to loop five times through a sequence
of statements with one Read statement in it.  The three integer
values are read in and stored in their respective variables with
the one statement.  If less than three are entered at the keyboard,
only as many as are read in will be defined, the rest will be
unchanged.  Following completion of the first loop, there is a
second loop in lines 19 through 25 that will be executed 5 times
with only one minor change, the Read statement is replaced by the
Readln statement.  At this point it would be best run this program
trying several variations with input data.

When you run READINT.PAS, it will request three integers.  Reply
with three small integers of your choice with as many blank spaces
between each as you desire, followed by a carriage return.  The
system will echo your three numbers back out, and request three
more.  Respond with only one number this time, different from each
of the first three, and a carriage return.  You will get your new
number followed by your previous second and third number indicating
that you did not re-enter the last two integer variables.  Enter
three more numbers, this time including a negative number and
observe the echo once again.

Continue entering numbers until the system outputs the message
indicating that it will now be using the Readln for reading data. 
At this point enter the same numbers that you did in the previous
section and notice the difference, which is only very slight.  Each
time you hit the enter key to cause the computer to process the

                                                        Page 10-2

                               Chapter 10 - Standard Input/Output

data you have just given it, it will echo the carriage return to
the display, and the "Thank you" message will be on a new line. 
When entering data from the keyboard, the only difference in Read
and Readln is whether or not the carriage return is echoed to the
display following the data read operation.

It should not be a surprise to you that after you enter the data,
the data is stored within the program and can be used anywhere that
integer data is legal for use.  Thus, you could read in an integer,
and use the integer to control the number of times through a loop,
as a case selector, etc.



TIME TO CRASH THE COMPUTER
_________________________________________________________________

Crashing the computer will not hurt a thing.  Rerun the above
program and instead of entering integer data, enter some real data
with decimal points, or even some character data.  The computer
should display some kind of message indicating that you have caused
an I/O error (Input/Output), and TURBO Pascal will abort operation
(that simply means to stop the program and return control to the
operating system).  No harm has been done, simply start it again
to enter more numbers or errors.



READING REAL NUMBERS
_________________________________________________________________

The example program READREAL.PAS will illustrate ================
how to read real numbers into the computer.  It   READREAL.PAS
will read an integer and three real numbers each ================
time through the loop.  It is perfectly fine to
give the system a number without a decimal point
for a real number.  The computer will simply read it as a decimal
number with zeros after the decimal point and consider it as a real
number internally. As you found out in the last example program,
however, it is not permissible to include a decimal point in the
data if the computer is looking for an integer variable.  Include
some character data for a real number and crash the system in this
program too.



READING CHARACTER DATA
_________________________________________________________________

The next example program, READCHAR.PAS, will     ================
read in one character each time through the loop   READCHAR.PAS
and display it for you.  Try entering more than  ================
one character and you will see that the extra
characters will simply be ignored.  It is not

                                                        Page 10-3

                               Chapter 10 - Standard Input/Output

possible to crash this program because any character you enter will
be valid.

Finally, READSTRG.PAS will also read up to 10    ================
characters, but since a string is a dynamic        READSTRG.PAS
length variable, it will only print out the      ================
characters you input each time, up to the
maximum of 10 as defined in the var declaration. 
It will display trailing blanks if you type them in because blanks
are valid characters.



BULLET PROOF PROGRAMMING
_________________________________________________________________

It can be frustrating to be running a program and have it declare
an I/O error and terminate operation simply because you have
entered an incorrect character.  The integer and real data inputs
defined earlier in this chapter are fine for quick little programs
to do specific calculations, but if you are writing a large
applications program it is better to use another technique.  Since
the character and string inputs cannot abort operation of the
program, it is best to use them to input the variable data and
check the data internally under your own program control.  An error
message can then be given to the operator and another opportunity
granted to input the correct data.  All well written large
application programs use this technique.



HOW DO I PRINT SOMETHING ON THE PRINTER?
_________________________________________________________________

With all of the Pascal knowledge you now have,   ================
it is the simplest thing in the world to get       PRINTOUT.PAS
data to the printer.  The example file           ================
PRINTOUT.PAS will show you graphically how to do
it.  Every Write or Writeln statement is
required to have a device identifier prior to the first output
field.  If there is none, it is automatically defaulted to the
standard output device, the display monitor.  The example program
has a few outputs to the monitor in lines 9 and 10 with the device
identifier included, namely Output.  This is only done to show you
the general form of the Write statements, but if you desire, you
can add the standard device identifier to every monitor output.

There are many statements in this program with the device
identifier Lst, which is the standard name for the list device or
the printer.  It should be obvious to you that the first field is
the device selector which is used to direct the output to the
desired device.



                                                        Page 10-4

                               Chapter 10 - Standard Input/Output

Compile and run this program with your printer turned on for some
printer output.  Just to supply you with a bit more information,
every Read and Readln statement is also required to have a device
identifier prior to the first input field.  As you may suspect, it
is also defaulted to Input if none is specified, and the standard
input device is the keyboard.



PROGRAMMING EXERCISE
_________________________________________________________________

1.   Write a program containing a loop to read in a character
     string up to 60 characters long, then print the string on your
     printer. When you run the program, you will have the simplest
     word processing program in the world. Be sure to include a
     test to end the loop, such as when "END" is typed in.






































                                                        Page 10-5
