From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_00,TO_NO_BRKTS_FROM_MSSP autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f19c6c58ce93435 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2001-12-11 08:33:05 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!cpk-news-hub1.bbnplanet.com!chcgil2-snh1.gtei.net!cambridge1-snf1.gtei.net!news.gtei.net!bos-service1.ext.raytheon.com!dfw-service2.ext.raytheon.com.POSTED!not-for-mail Message-ID: <3C1634E3.D96DA582@west.raytheon.com> From: Jerry Petrey <"jdpetrey"@west.raytheon.com> X-Mailer: Mozilla 4.76 [en]C-CCK-MCD CSC;Raytheon (WinNT; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Error checking for type incompatibility References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Tue, 11 Dec 2001 09:31:31 -0700 NNTP-Posting-Host: 147.24.83.38 X-Complaints-To: news@ext.ray.com X-Trace: dfw-service2.ext.raytheon.com 1008088385 147.24.83.38 (Tue, 11 Dec 2001 10:33:05 CST) NNTP-Posting-Date: Tue, 11 Dec 2001 10:33:05 CST Organization: Raytheon Company Xref: archiver1.google.com comp.lang.ada:17764 Date: 2001-12-11T09:31:31-07:00 List-Id: 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 in email address to reply -----------------------------------------------------------------------------