Information cannot be manipulated by a computer unless it is first stored in main memory. There are three ways to place a data value in memory: Associate it with a constant, assign it to a variable, or read it into a variable from the terminal or a file. The first two approaches can be followed only when the value to be stored will be the same every time the program is run. If we wish to be able to store different information each time, it must be read in as the program is executing (an input operation).
As it executes, a program performs computations and assigns new values to variables. The results of a program's execution can be displayed to the program user by an output operation.
Input/output operations in Ada are performed by procedures that are
included in a set of library packages supplied with each Ada compiler.
We will use procedures from Ada.Text_IO
,
Ada.Integer_Text_IO,
and Ada.Float_Text_IO
; later we
will use other parts of the input/output libraries. The specific procedure used
to read or display a value is determined by the type of that value. For
the time being, we will manipulate values of four different types: character,
string, floating-point number, and integer. As you write each program, you
should be aware of the input/output operations that need to be performed and
give the required context clauses. Input/output operations in Ada are done
using procedure calls, so we now present a syntax display that shows the form
of a call.
SYNTAX DISPLAY
Simple Procedure Call Statement
pname (list of parameters);
Ada.Text_IO.Put(Item => "Hello."); Ada.Text_IO.New_Line;
Item
. There is no special Ada rule that requires
this; it is just the name chosen by the designers of Ada.Text_IO
.
As the second example shows, it is possible for a procedure to require no parameters at all. The number, order, and type of parameters are, of course, determined by the writer of the procedure, not by its user.
Ada.Float_Text_IO.Get (Item => Inches);reads a floating-point number (a number with a decimal point) into the variable
Inches
. This statement causes the number entered at the
keyboard to be stored in the variable Inches
, as illustrated in
Figure
2.4. After typing a number, the program user should press the
RETURN
or ENTER
key or the space bar.
Figure 2.4
Ada.Float_Text_IO.Get(Item=>Inches);
Now recall that in Program 2.2, a user's initials were read. Because each person using the program probably will have different initials, the statements
Ada.Text_IO.Get (Item => Initial1); Ada.Text_IO.Get (Item => Initial2);are used to read in two letters. These statements cause the next two characters entered at the terminal to be stored in the variables
Initial1
and
Initial2
(type Character
), one character per variable.
Figure
2.5 shows the effect of these statements when the letters EK
are entered.
It may be necessary to press the RETURN
key after typing in the
data characters.. Some systems will read in these characters as they are typed;
most will not begin to read them until after the RETURN
key is
pressed.
Figure 2.5
The procedure Ada.Integer_Text_IO.Get
is used to read an
integer (a number without a decimal point). This number may or may not
be preceded by a sign. The variable into which this number is stored must be of
type Integer
.
The number of characters read by an input operation depends on the type of the
variable into which the data are placed. Only one character is read for a
variable of type Character
. In the case of integer and
floating-point values, the computer skips over any leading blanks and then
continues to read characters until a character that cannot be part of a number
is reached (e.g., a blank or a letter) or the RETURN
key is
pressed.
How does a program user know when to enter the input data and what data to enter? Your program should print a prompting message (as explained in the next section and as the examples have shown) to inform the program user what data to enter and when. The cursor indicates the current position on the video screen. As each character is entered, the cursor advances to the next screen position.
It is interesting to note that the four input characters in
Program
2.4 comprise a single data value, the number 30.5, which is stored in the
variable Inches
(type Float
). In
Program
2.2, each input character represents a separate data value and is stored in
a different variable. And in
Program
2.3, where a user's name is read, the sequence of exactly ten characters
represents a single value. A sequence of one or more characters representing a
single input value is commonly called a token. The input sequence
30.5
is a floating-point token, the sequence Jane
Smith
is a string token, and the initials JS
represent two
single-character tokens.
SYNTAX DISPLAY
Character Get Procedure
Ada.Text_IO.Get (Item => variable );
Ada.Text_IO.Get (Item => Initial1);
Character
). A blank counts as a
character; a RETURN
does not.SYNTAX DISPLAY
String Get Procedure
Ada.Text_IO.Get (Item => variable );
Ada.Text_IO.Get (Item => First_Name);
String (low..high)
, where 1 <= low <=
high. Exactly high - low + 1 characters are read from the keyboard. A
RETURN
does not count as a character; the computer will wait until
exactly the right number of keys, excluding RETURN
s, are
pressed.SYNTAX DISPLAY
Integer Get Procedure
Ada.Integer_Text_IO.Get (Item => variable
);
Ada.Integer_Text_IO.Get (Item =>How_Long);
Integer
). Any leading blank
characters or RETURN
s are ignored. The first nonblank character
may be a sign (+ or -) or a digit. The data string is terminated when a
nonnumeric character is entered or the space bar or RETURN
key is
pressed. SYNTAX DISPLAY
Floating-Point Get Procedure
Ada.Float_Text_IO.Get (Item => variable );
Ada.Float_Text_IO.Get (Item => Inches);
Float
). Any leading blank characters or RETURN
s are
ignored. The first nonblank character may be a sign (+ or -) or a digit; the
remaining characters must be either an integer token (if the value is a whole
number) or a single decimal point surrounded by numeric characters. The data
string is terminated when a character is entered that cannot be part of a
floating-point token, or the space bar or RETURN
key is pressed.
[1] In order to see the results of a program execution we must have some way of displaying the values of selected variables. In Program 2.4, the statements
Ada.Text_IO.Put (Item => "That equals "); Ada.Float_Text_IO.Put (Item => Centimeters); Ada.Text_IO.Put (Item => " centimeters."); Ada.Text_IO.New_Line;display the output line
That equals 7.74700E+01 centimeters.The procedure
Ada.Text_IO.Put
is called twice, first to display the
string "That equals"
and next to display the string "
centimeters."
. A string must be enclosed in double quotes. When the
Ada.Text_IO.Put
statement is executed, the characters enclosed in
the quotes are printed, but the quotes are not.
The procedure Ada.Float_Text_IO.Put
displays the value of variable
Centimeters
(type Float
) between two strings. The
number displayed is 77.47 expressed in scientific notation. In normal
scientific notation, 7.747 x 101 means multiply 7.747 by 10, or move
the decimal point right one digit. Because superscripts cannot be entered or
displayed at the terminal, the capital letter E
is used in
computers to indicate scientific notation.
In Program 2.2, the statements
Ada.Text_IO.Put (Item => Initial1); Ada.Text_IO.Put (Item => Initial2);display the characters stored in the two variables
Initial1
and
Initial2
(type Character
). Each statement causes a
single character to be displayed at the current cursor position.
If the variable given to Ada.Text_IO.Put
is of type
String(low..high)
as in
Program
2.3,
Ada.Text_IO.Put (Item => First_Name);exactly high - low + 1 characters are displayed.
The procedure Ada.Integer_Text_IO.Put
is used to display integer
values. Whenever an output operation is performed, the characters to be
displayed appear at the current cursor position.
The procedure Ada.Text_IO.New_Line
is used to segment our program
output into lines. Each time Ada.Text_IO.New_Line
is executed, the
cursor is advanced to the first position of the next line on the screen.
Ada.Text_IO.Put (Item => "Enter your two initials >");and
Ada.Text_IO.Put (Item => "Enter a length in inches >");are both used to display prompts or prompting messages in Program 2.2 and Program 2.4, respectively. A prompting message is a string that is displayed just before an input operation is performed. Its purpose is to request that the program user enter data; it may also describe the format of the data expected. It is very important to precede each input operation with an
Ada.Text_IO.Put
statment that displays a prompt; otherwise, the
program user may have no idea that the program is waiting for data entry or
what data to enter.
Program output is usually designed to be read by humans from a screen display or a printed report. It is therefore important that the output be formatted or organized in a way that makes it most easily understood. For example, the decimal value 77.47 is much more obvious to most people than the scientific-notation form of the same value, 7.74700E+01. Also, displays and reports should be organized in nice neat columns so that the information in them is easily digested by the human reader.
Programming languages facilitate production of useful reports by
providing ways
of precisely controlling both the form and the width of output values,
especially numerical ones. In the case of Ada, the integer and floating-point
Put
procedures provide additional parameters for output
formatting. These are values that are supplied in the procedure call
statement.
The integer Put
procedure allows one additional parameter called
Width
, which indicates the number of print positions to be used
for the output value. The statement
Ada.Integer_Text_IO.Put (Item => How_Far, Width => 4);will right-adjust the displayed value of
How_Far
to four
positions. This means that if How_Far
is 327, when the value is
displayed, it will be preceded by one blank. If How_Far
is 19, it
will be preceded by two blanks; if How_Far
is 1024, it will be
preceded by no blanks at all.
Now suppose that How_Far
is 12000, which would be a very long
trip! In that case, the field in the display would be extended to five
positions so that no important information would be lost.
In
Program
2.5 the output statement supplied no value for Width
at all.
Ada permits the omission of procedure parameters, but only if the author of the
procedure has supplied a default value, which will be used instead. The
integer Put
comes with a default value for Width
, but
this value can vary from compiler to compiler. This is why in the remaining
programs in this book, a value for Width
will usually be supplied
in the procedure call. We recommend that you follow this practice as well
because it makes your programs more portable (independent of a particular
compiler).
In the case of floating-point output, Ada provides for three formatting parameters:
Fore
, which indicates the number of positions before the
decimal point,
Aft
, which indicates the number of positions after the decimal
point, and
Exp
, which indicates the number of positions
desired following the E. If Exp is 0, no exponent will apear at all; this
produces a decimal value, rather than a scientific-notation one.
Look again at Program 2.4. If we change the output statement to
Ada.Float_Text_IO.Put (Item=>Centimeters, Fore=>5, Aft=>2, Exp=>0);will produce the value 77.47 preceded by three blanks.
SYNTAX DISPLAY
Character Put Procedure
Ada.Text_IO.Put (Item => variable );
Ada.Text_IO.Put (Item => Initial1);
Character
) is displayed on the
screen and the cursor is advanced to the next position.SYNTAX DISPLAY
Ada.Text_IO.Put (Item => variable );
Ada.Text_IO.Put (Item => First_Name);
String (low..high)
,
where 1 <= low <= high. Exactly high - low + 1
characters are displayed on the screen, and the cursor is advanced to the first
position after the end of the string.SYNTAX DISPLAY
Ada.Integer_Text_IO.Put (Item => variable , Width =>
field width );
Ada.Integer_Text_IO.Put (Item => How_Long, Width => 5);
Integer
) is displayed, using
the next Width
positions on the screen. If the value (including
sign) occupies less than Width
positions, it will be preceded by
the appropriate number of blanks; if the value occupies more than
Width
positions, the actual number of positions is used. If
Width
is omitted, a compiler-dependent width is used by
default.
Table 2.3 shows some examples of formatted integer values.
Table
2.3
Formatted Integer Values
Value Width Displayed Output 234 4 234 234 5 234 234 6 234 -234 4 -234 -234 6 -234 234 Len 234 (if Len is 6) 234 1 234 234 0 234
SYNTAX DISPLAY
Ada.Float_Text_IO.Put (Item => variable , Fore => width
before point , Aft => width after point , Exp => width of
exponent );
Ada.Float_Text_IO.Put (Item => Inches, Fore => 5, Aft => 2, Exp=> 0);
Fore
gives the desired number of positions in the integer part (to the left of the
decimal point); Aft
gives the number of positions in the
fractional part (to the right of the decimal point); Exp
gives the
desired number of positions in the exponent (after the E). If the exponent
value, including sign, occupies fewer than Fore
positions, blanks
are added on the left. If Exp
is 0, no exponent is displayed.Table 2.4 shows some examples of formatted floating-point values.
Table
2.4
Formatted Floating-Point Values
Value Fore Aft Exp Displayed Value 3.14159 2 2 0 3.14 3.14159 1 2 0 3.14 3.14159 3 1 0 3.1 3.14159 1 3 0 3.142 3.14159 2 5 0 3.14159 3.14159 1 3 2 3.142E+00 0.1234 1 2 0 0.12 -0.006 1 2 0 -0.01 -0.006 1 2 2 -6.00E-3 -0.006 1 5 0 -0.00600 -0.006 4 3 0 -0.006It is very important to realize that these are just different ways of formatting output values, i.e., controlling the visible form of these values on the screen. Nothing in these output statements alters the actual value stored in memory.
SYNTAX DISPLAY
Ada.Text_IO.New_Line (Spacing => positive number );
Ada.Text_IO.New_Line ( Spacing => 3 );
Spacing
is 1, the cursor is moved to the first position of the
next line of the display. If Spacing
is greater than 1, this
action is performed Spacing
times. If Spacing
is
omitted, 1 is used as the default.
PROCEDURE SMALL; X : Float; Y : Foat; X : Float; BEGIN; 15.0 = Y; Z= -Y + 3.5; Y + z = x; Put(x, Y, z) end small;
The value of X is ---------- pounds.
Display the value of X
using ten characters in the
space provided.
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.