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-Thread: 103376,4819e43c270862a9 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!z16g2000prd.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Newbie Question: Integer_IO an Data_error Date: Thu, 26 Mar 2009 17:03:06 -0700 (PDT) Organization: http://groups.google.com Message-ID: <13affeba-f5f5-43a5-a476-9d486b1c4517@z16g2000prd.googlegroups.com> References: <873ad0ueva.fsf@babel.localdomain> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1238112186 6780 127.0.0.1 (27 Mar 2009 00:03:06 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 27 Mar 2009 00:03:06 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: z16g2000prd.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:5325 Date: 2009-03-26T17:03:06-07:00 List-Id: On Mar 26, 3:18 pm, Tim Rowe wrote: > Zachary Kline wrote: > > The problem comes with Data_errors: if I try to > > handle that case the same way, we get a seemingly infinite loop. > > I'm an Ada newbie too, but the first thing I wonder is what Get does > with the input characters if they don't match the expected type. I > wonder whether it leaves them on the input stream -- in which case > subsequent attempts to read the input will try to read the same > characters each time. Until somebody who knows what they're talking > about comes along (won't be long) I'm sure you're right, that it won't be long. In the meantime, you're stuck with me. Anyway, A.10.8(8) says that Get (in Integer_IO), when Width=0, "reads the longest possible sequence of characters matching the syntax of a numeric literal without a point". [I presume they mean "decimal point" and aren't making any commentary about whether the number you read will be useful or not.] The way I interpret this, if the first character in the input stream (past any leading blanks) is something other than a digit or a sign, the longest possible sequence is zero characters. It's not 100% clear, since a zero-character sequence doesn't match the syntax of a numeric literal either, but that's what I think should happen. So if it can't read any integer, it will leave everything in the input stream (other than leading blanks). I think that if there's a sign followed by garbage, it will swallow up the sign before raising Data_Error, though. So this does result in an "infinite" loop, although that won't happen the way the OP coded it: exception when Constraint_Error => Put_Line ("Please, numbers from one to a hundred only."); Put ("Your guess: "); Get (Target); when Data_Error => Put_Line ("Numbers only, please."); Put ("Your guess: "); Get (Target); The Get calls that appear in the exception handler are *not* controlled by the exception handlers. So if another Constraint_Error or Data_Error occurs in those Get calls, it won't loop back and execute the same exception handler, but will rather look for an *outside* exception handler that catches the exceptions, or die if there isn't one. To write this properly, you'll need to write a loop, perhaps Ch : Character; begin Input_Loop: loop begin Put ("Your guess: "); Get (Target); exit Input_Loop; exception when Constraint_Error => Put_Line ("Please, numbers from one to a hundred only"); when Data_Error => Put_Line ("Numbers only, please"); Get (Ch); -- or something else end; end loop Input_Loop; end Get_Guess; I'd try doing a string Get after a > failed numeric get, to see what's still in the input stream. Get(Ch) would eat the next character in the input stream if there's a Data_Error; more likely, you'd want to say Skip_Line to throw away everything in the rest of the line, but that depends on how you want the program to work. -- Adam