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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8dd8ee71ca4e5206 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-03 19:42:42 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news-out.nuthinbutnews.com!propagator2-sterling!news-in-sterling.newsfeed.com!news-in.nuthinbutnews.com!cyclone1.gnilink.net!wn11feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!sccrnsc03.POSTED!not-for-mail From: "SteveD" Newsgroups: comp.lang.ada References: <93d4dcd4.0210031020.b0cca2b@posting.google.com> Subject: Re: Newbie question on Ada TExt_IO X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Message-ID: NNTP-Posting-Host: 12.225.227.101 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc03 1033699361 12.225.227.101 (Fri, 04 Oct 2002 02:42:41 GMT) NNTP-Posting-Date: Fri, 04 Oct 2002 02:42:41 GMT Organization: AT&T Broadband Date: Fri, 04 Oct 2002 02:42:41 GMT Xref: archiver1.google.com comp.lang.ada:29506 Date: 2002-10-04T02:42:41+00:00 List-Id: "Justin" wrote in message news:93d4dcd4.0210031020.b0cca2b@posting.google.com... > Hi, [snip] > > X:Integer; > ... > Put("Please enter a number between 1 and 6); > Get(x); > > But what if they give me 'abc' or 4.44 or simply > 1230000000000000000000000000000000000000000000000000000000000000000000000 > > How can I handle this? From my take on what I've read I should avoid > exception handling for things I'm not expecting, so I've ruled out > exceptions, that leaves me with obtaining a value of generic type and > evalutating the type at run-time...how can I do this? Is this the > right strategy? I suggest that you handle this differently depending on the target audience (or "user" if you prefer). If I am putting together a small program for internal use, I typically use a small loop with an exception handler... something along the lines of (pseudo code): loop begin Prompt for input Get input Exit when input is valid exception when others => Display a nastygram about invalid input end; end loop; skip to the next line of input If I am putting something together for external use, I would read the value into a string using something like Text_Io.Read_Line with the string ridicuously large (maybe 1..256). If after reading the string the value of "last" is the size of the string, I know something is wrong and report an error (unlikely to happen, but handled). Once I get a reasonably sized string, I trim leading and trailing spaces using Ada.Fixed.Trim. Then I check for valid characters using something like Ada.Strings.Fixed.Index to make sure the numeric input contains only digits. Then I use the Integer'Value to get the actual value of the numeric string. Just to be safe the Integer'Value conversion is protected by an exception handler. These days most programs for external use are GUI's, which change the rules a bit but still require validation. I hope this helps, SteveD