Previous | Next | Table of Contents | Index | Program List | Copyright

2.3 System Structures: Numerical Input and Output

So far, our program examples have used only character and string quantities. Computers are commonly used to work with numbers, so it is time for a numerical example or two. Computer programs use two general kinds of numerical values: integer values, such as 0, 2, and -1048, which are "whole numbers" with no fractional part, and floating-point values, such as 0.0, 3.14159 and -185.7, which are numbers with fractional parts. Ada requires us, generally, to keep integer numbers and floating-point numbers separate and not to mix them in the same calculation.

In Ada, reading numerical values from the keyboard or a file, and writing or displaying these, are done by using two important components of the standard Ada libraries, called Ada.Integer_Text_IO and Ada.Float_Text_IO. You now know of three input/output packages. If your program reads or displays ordinary characters and strings or uses New_Line, precede your program with a context clause:

    WITH Ada.Text_IO;
If your program reads and displays integer quantities, precede it by
    WITH Ada.Integer_Text_IO;
If your program reads and displays floating-point quantities, precede it by
    WITH Ada.Float_Text_IO;
It is permissible to have two, or even all three, context clauses, if necessary.

Example 2.4

Program 2.4 converts inches to centimeters.

Program 2.4
Converting Inches to Centimeters

WITH Ada.Text_IO;
WITH Ada.Float_Text_IO;
PROCEDURE Inch_to_CM IS
------------------------------------------------------------------------
--| Converts inches to centimeters
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: July 1995                                     
------------------------------------------------------------------------

  CM_Per_Inch : CONSTANT Float := 2.54;
  Inches      : Float;
  Centimeters : Float;

BEGIN -- Inch_to_CM

  Ada.Text_IO.Put (Item => "Enter a length in inches> ");
  Ada.Float_Text_IO.Get (Item => Inches);

  Centimeters := CM_Per_Inch * Inches;

  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;

END Inch_to_CM;
Sample Run
Enter a length in inches> 30.5
That equals  7.74700E+01 centimeters

The number of inches to be converted is read into the memory cell Inches by the statement

    Ada.Float_Text_IO.Get (Item => Inches);
The Get statement looks similar to the one in the earlier examples. There are many different Get statements in the input/output libraries; they have in common the fact that each is able to accept keyboard input and store it in a single data element. As before, we write the prefix Ada.Float_Text_IO to indicate that we are interested in the Get supplied by the floating-point input/output package.

The statement

    Centimeters := CM_Per_Inch * Inches; 
computes the equivalent length in centimeters by multiplying the length in inches by the floating-point constant 2.54 (the number of centimeters per inch); the product is stored in memory cell Centimeters.

The statement

    Ada.Float_Text_IO.Put (Item => Centimeters);
displays the value of Centimeters as the floating-point number 7.74700E+01 in Ada scientific notation. The value printed is equivalent to 7.747 x 10, or 77.47, as will be explained later.

Suppose the user enters a negative number of inches at the keyboard (say, -1.45). The program will compute a negative number of centimeters. Whether this is appropriate or not depends on the use we are making of the program. Throughout this book we will be introducing better and better ways of ensuring that user input is appropriate before proceeding to a calculation that may not make sense. At this stage we can only identify the problem; we do not yet have the tools to solve it.

Example 2.5

Program 2.5 computes the distance of an automobile trip by asking the user to enter the estimated trip time in hours and the average speed in miles per hour.

Program 2.5
Finding Distance Traveled

WITH Ada.Text_IO;
WITH Ada.Integer_Text_IO;
PROCEDURE Distance IS
------------------------------------------------------------------------
--| Finds distance traveled, given travel time and average speed  
--| Author: Michael B. Feldman, The George Washington University 
--| Last Modified: July 1995                                     
------------------------------------------------------------------------

  How_Long : Natural;
  How_Fast : Natural;
  How_Far  : Natural;
 
BEGIN -- Distance

  -- prompt user for hours and average speed
  Ada.Text_IO.Put 
    (Item => "How many hours will you be driving (integer) ? ");
  Ada.Integer_Text_IO.Get (Item => How_Long);
  Ada.Text_IO.Put 
    (Item => "At what average speed (miles per hour, integer) ? ");
  Ada.Integer_Text_IO.Get (Item => How_Fast);
  
  -- compute distance driven
  How_Far := How_Fast * How_long; 
   
  -- display results
  Ada.Text_IO.Put (Item => "You will travel about ");
  Ada.Integer_Text_IO.Put (Item => How_Far);
  Ada.Text_IO.Put (Item => " miles");
  Ada.Text_IO.New_Line;

END Distance;
Sample Run
How many hours will you be driving (integer) ? 3
At what average speed (miles per hour, integer) ? 55
You will travel about         165 miles

The numbers are nonnegative integer values (type Natural). Nonnegative integers are still integers, so we can make use of the integer input/output package Ada.Integer_Text_IO, calling the Get and Put operations there.

In Programs 2.4 and 2.5, there are two context clauses (WITH clauses) preceding the program. Why do we need both? Because we are displaying prompts to request user input as well as titles to make the output meaningful, we need to use the character-string part of Ada.Text_IO to do this, in addition to the appropriate numerical input/output package. Ada requires us to supply context clauses for all library packages we are using.

In testing this program, we entered positive numbers for the trip time and speed. You might find it interesting to execute the program yourself and enter a negative number. The result will be a message from Ada called an "exception report." Exception reports will be discussed in Section 2.11.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.