Previous topic | Ada Home Page | Index

Safe float I/O

    procedure gen_float_input( out_float : out FLOAT;
                               min, max  : in  FLOAT) is

       local_float : FLOAT;          -- local input var
       good_one    : BOOLEAN := FALSE; -- loop control

    begin -- gen_float_input
        while not good_one loop
        begin -- protected block of code
            PUT("Enter a float in range ");
            PUT( min, EXP => 0 ); PUT( " to ");
            PUT( max, EXP => 0 ); PUT( " ");
            GET( local_float );

            -- this point can only be reached if the get
            -- did not raise the exception

            -- now tested against limits specified
            good_one:=((local_float>=min) and (local_float<=max));

            if not good_one then
                raise DATA_ERROR;
            end if;

        exception
            when DATA_ERROR =>
                PUT_LINE("Invalid input, please try again ");
                SKIP_LINE;
        end; -- protected block of code
        end loop;

        -- this point can only be reached when valid value input
        SKIP_LINE;             -- tidy up terminal handling

        out_float := local_float;  -- export input value

    end gen_float_input;


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 21 Feb 96