Back to DFS's Turing Page


Keyboard Input I

To write an interactive program, you obviously need a method for requesting information from the user and another for receiving the information. When a program waits for the user to type something, it is customary to provide a prompt, a cue to the user that he is expected to do something, such as Insert Coin at the arcade.

The program below, a reworking of the earlier area-of-a-triangle program, includes several new things:

  1. Simple introduction and instructions for the user;
  2. Labelled separators to break up the program visually;
  3. A programming technique for getting information from the user.

Please take note of the fact that the command that produces the prompt is a put.., not a put. The two dots (..) cause the cursor to remain on the prompt line while the program awaits the user's response. Note also that the prompt string ends with a wordspace after the question mark so that the response does not abut the prompt.

% Interactive program to calculate the area of a triangle
var b: int %base
var h: int %height
var A: real %Area
%-------------------Introduction------------------
put "This program calculates and prints the area of a triangle"
put "after you enter its dimensions."
put "When asked to, type in a dimension and hit the ENTER key."
%-------------------Inputting Data------------------
put "What is the length of the triangle's base? "..
get b
put "What is the triangle's height? "..
get h
%---------------------Calculation------------------
A := b * h / 2
%-------------------Print the result---------------
put "The area of a triangle with a base of ", b, " units and"
put "a height of ", h, " units is ", A:0:1, " square units."

© 1998-2004 DFStermole
Last updated 16 Feb 2004