comp.lang.ada
 help / color / mirror / Atom feed
From: Jerry Petrey <"jdpetrey<NOSPAM>"@west.raytheon.com>
Subject: Re: Error checking for type incompatibility
Date: Tue, 11 Dec 2001 09:31:31 -0700
Date: 2001-12-11T09:31:31-07:00	[thread overview]
Message-ID: <3C1634E3.D96DA582@west.raytheon.com> (raw)
In-Reply-To: bf8dbe70.0112101628.1092b75a@posting.google.com



Ben wrote:
> 
> Hi,
> I am new to Ada. I decided I would try to learn a new language and Ada
> seemed like a fun project. I got a few books, and one of them has
> challenge problem where I am supposed to do some error checking on
> input. This includes checking to make sure that the input type is
> correct. More specifically: the program is supposed to accept only
> integer values. So I need to be able to have an error handler that
> will recognize if someone tries to enter a noninteger value and ask
> them to re-enter a correct value. Since this is a "challenge problem"
> they are not giving me alot to work on here. I checked another book,
> and it had some examples on error handling, but not at the level I
> need. And neither book had any example code that was helpful.
> Remember, I'm new to Ada (from the C++ world) so packages are new to
> me. What do I need folks. Your help is greatly appreciated.
> 
> Thanks,
> Ben


Ben, here is a simple example of error handling from a program
I wrote years ago.  Of course you won't have some of the
subprograms that it uses but hopefully it will show you 
the basic concept and structure for input error handling.
It expects the user to enter 4 hex digits and prints out
the decimal value and what that represents in voltage in
this particular application.  The procedure HEX_GET returns 
an ERROR flag if the numbers entered are not valid hex digits,
in which case the exception ENTRY_ERROR is raised and the 
exception handler (inside the loop) prints out the retry 
message and control returns to the end of the block 
(still inside the loop) so the HEX_GET is called again. 
Hope this helps.

Jerry

procedure Input_Hex_Number is
   loop
      ERASE_SCREEN;
      --
      CURSOR_TO(3, 1);
      PUT(" Enter four digit Hex number: ");
      loop
         begin
	    HEX_GET(N, 4, ERROR);
            if ERROR then
               raise ENTRY_ERROR;
            end if;
            exit;
         exception
            when ENTRY_ERROR =>
               --
               CURSOR_TO(3, 1);
               ERASE_LINE;
               PUT("Please re-enter four digit Hex number again, carefully:
");
         end;
      end loop;
      CURSOR_TO(5, 1);
      PUT("You entered Hex ");
      HEX_PRINT(N, 4);
      PUT(" which is ");
      PUT(N, 3);
      PUT(" Decimal");
      NEW_LINE;
      --
      VOLTAGE := CONVERT(LONG_INTEGER(N));
      PUT(" This Value = ");
      PUT(VOLTAGE, 2, 3, 0);
      PUT(" Volts");
      NEW_LINE;
      --
      PUT(" Again? (y or n) ");
      GET(C);
      SKIP_LINE;
      exit when C /= 'y';
   end loop;

end Input_Hex_Number;


-- 
-----------------------------------------------------------------------------
-- Jerry Petrey                                                
-- Senior Principal Systems Engineer - Navigation, Guidance, & Control
-- Raytheon Missile Systems          - Member Team Ada & Team Forth
-- NOTE: please remove <NOSPAM> in email address to reply                  
-----------------------------------------------------------------------------



      parent reply	other threads:[~2001-12-11 16:31 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-12-11  0:28 Error checking for type incompatibility Ben
2001-12-11  0:49 ` Mark Lundquist
2001-12-11  7:35 ` Preben Randhol
2001-12-11 16:31 ` Jerry Petrey [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox