2.11 Interactive Programs

The sample programs given thus far all produced a single output. Their data was right in the code, either as literals that were written out, or as constants that were operated on to produce an output. It is all very well to have a program produce some answers, but communication ought to be a two-way street. Programs are much more versatile if they can print out messages to the user asking for input data, read the answers typed into variables, and then act on this data. The procedures to achieve the reading of data are also located in the modules STextIO, SWholeIO, and SRealIO. (Or from InOut and RealInOut) The following are among the entities available for IMPORT.

From STextIO or InOut

ReadChar--takes in one character and puts it into CHAR variable memory (If from InOut, called just Read)

From SWholeIO or InOut

ReadInt--accepts and assigns an INTEGER

ReadCard--does the same for a CARDINAL

From SRealIO or RealInOut (or possibly from InOut)

ReadReal--ditto for a REAL

Before giving examples, two definitions are in order:

A program that operates on a set of fixed data obtained either from within the program itself or from a file is said to be a batch style program.
A program that interacts with its user via prompts to obtain the data on which its execution depends is called interactive.

Here, for instance are two earlier batch style examples re-written in interactive style:

Example:

Write a module to convert user-supplied Fahrenheit temperatures to Celsius units.

MODULE ConvertToCelsiusI;
(* by R. Sutcliffe *)
(* to illustrate interactive style *)

FROM STextIO IMPORT
  WriteLn, WriteString, WriteChar;
  (* The latter is called simply "Write" when in InOut. *)

FROM SRealIO IMPORT
  ReadReal, WriteReal;

CONST
  conversion = 32.0;  (* difference between zero points *)
  ratio = 5.0/9.0;    (* ratio between Celsius and Fahrenheit *)

VAR
  Fahrenheit, Celsius : REAL;

BEGIN
  (* ask user for data *)
  WriteString ("What is the Fahrenheit value to convert? ");
  ReadReal (Fahrenheit);
  WriteLn;
  (* do the calculation *)
  Celsius := ratio * (Fahrenheit - conversion);
  WriteReal (Fahrenheit, 12);
  WriteChar ('F');
  WriteString (" degrees equals ");
  WriteReal (Celsius, 12);
  WriteChar ('C');
  WriteString (" degrees.");

END ConvertToCelsiusI.

The output from this program module is as follows: (user input in boldface)

What is the Fahrenheit value to convert? 14.0
14.0000000000F degrees equals -10.000000000C degrees.

Example:

Write a module to compute the area and circumference of a circle from a user-supplied radius.

MODULE CircleAreaCircI;
(* by R. Sutcliffe *)
(* to illustrate interactive style *)

FROM STextIO IMPORT
  WriteLn, WriteString;

FROM SRealIO IMPORT
  ReadReal, WriteReal;

VAR
  radius, area, circumference : REAL;

CONST
   pi = 3.1415926;
   twopi = 2.0 * pi;

BEGIN
(* ask user for data *)
  WriteString ("What is the radius of the circle? ==>");
       (* fancier prompt *)
  ReadReal (radius);
  WriteLn;
(* calculation *)
  area := pi * radius * radius;
  circumference := twopi * radius;
(* output *)
  WriteString ("A circle of radius ");
  WriteReal (radius, 0);
  WriteString (" units has a circumference of ");
  WriteReal (circumference, 0);
  WriteString (" units");
  WriteLn;
  WriteString (" and an area of ");
  WriteReal (area, 0);
  WriteString (" square units.");
END CircleAreaCircI.

The output from this program module is as follows: (user input in boldface)

What is the radius of the circle? ==> 13.0
A circle of radius  1.3000000E+1 units has a circumference of  8.1681404E+1 units
 and an area of  5.3092914E+2 square units.

Contents