The following example shows the use of some of the text input-output facilities in a dialogue with a user at a terminal. The user is prompted to type a color, and the program responds by giving the number of items of that color available in stock, according to an inventory. The default input and output files are used. For simplicity, all the requisite instantiations are given within one subprogram; in practice, a package, separate from the procedure, would be used.
with TEXT_IO; use TEXT_IO; procedure DIALOGUE is type COLOR is (WHITE, RED, ORANGE, YELLOW, GREEN, BLUE, BROWN); package COLOR_IO is new ENUMERATION_IO(ENUM => COLOR); package NUMBER_IO is new INTEGER_IO(INTEGER); use COLOR_IO, NUMBER_IO; INVENTORY : array (COLOR) of INTEGER := (20, 17, 43, 10, 28, 173, 87); CHOICE : COLOR; procedure ENTER_COLOR (SELECTION : out COLOR) is begin loop begin PUT("Color selected: "); -- prompts user GET(SELECTION); -- accepts color typed, or raises exception return; exception when DATA_ERROR => PUT("Invalid color, try again. "); -- user has typed NEW_LINE(2); -- new line -- completes execution of the block statement end; end loop; -- repeats the block statement until color accepted end; begin -- statements of DIALOGUE; NUMBER_IO.DEFAULT_WIDTH := 5; loop ENTER_COLOR(CHOICE); -- user types color and new line SET_COL(5); PUT(CHOICE); PUT(" items available:"); SET_COL(40); PUT(INVENTORY(CHOICE)); -- default width is 5 NEW_LINE; end loop; end DIALOGUE;
Example of an interaction (characters typed by the user are italicized):
Color selected: Black Invalid color, try again. Color selected: Blue BLUE items available: 173 Color selected: Yellow YELLOW items available: 10
Address any questions or comments to firstname.lastname@example.org.