Assignment 5 for CSCI 431

Overview

Ada has extensive block structuring capability. The purpose of this lab is to investigate a few of the different types of blocks available. Specifically, you will use the Ada block statement to handle exceptions without aborting a program.

Background

The program lab301.adb is a simple program in which the user is prompted to type positive integers which are then read and stored in successive locations of a ten element array. Entering a zero terminates the input and then the integers are printed out.

Try entering more than 10 different integers---this will cause a run time error

The program lab302.adb handles the error that you encountered with program lab301.adb. The loop used in reading the integers is placed inside of an Ada block statement. This allows the exception causing the run-time error to be handled within the block so that the program can continue to run without aborting. Compile and run this program entering more than 10 integers to verify that the above statement is true.

Now run lab302.adb and enter non-numeric data---this will cause yet another run time error.

Turn In

Rewrite lab302.adb so that it does the following:

  1. Handles the exception caused by entering non-numeric data by printing an appropriate message and then continuing to prompt for and enter integers into the array.
  2. It raises and handles an exception when a negative integer is entered (also make the prompt request a positive integer)
  3. The variable number used to read the integer from standard input and the exception raised when a negative number is read are local to the block of code in which they are used, that is, local to the code which inputs the integers.

Hint

Remember the instructions for compiling and running Ada programs from assignment 4. When you complete part 3 of your assignment, your program should look something like:



with text_io; use text_io;

   --   file: lab302.adb
   --
   --   Lab 3.1 - Ada
   --   Blocks
   --   
   --   This program is used to investigate blocks in Ada.
   --   A list of up to ten integers is read into an array
   --   and then printed out.  Blocks are used to handle
   --   exceptions locally so that the program does not abort.

procedure lab302 is

    package int_io is new integer_io(integer); use int_io;

    type vector is array(1..10) of integer;

    list       : vector;          -- Storage for the list of integers
    size       : integer := 0;    -- Size of the list

begin

begin
    loop
        put("enter a positive integer (0 to end input) ");

        begin

         ...

        exception

        ....

        end;
    end loop;
end; 

-- Now output the list which was just read in, two integers per line.

new_line;
put_line("The integers in the list are:");
for i in 1..size loop
    put(list(i));
    if (i mod 2 = 0) then
        new_line;
    end if;
end loop;
new_line;
end lab302;