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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!news3.google.com!feeder.news-service.com!news.netcologne.de!newsfeed-fusi2.netcologne.de!newsfeed-0.progon.net!progon.net!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Newbie Question: Integer_IO an Data_error Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <873ad0ueva.fsf@babel.localdomain> Date: Fri, 27 Mar 2009 09:54:29 +0100 Message-ID: NNTP-Posting-Date: 27 Mar 2009 09:54:29 CET NNTP-Posting-Host: c8c081ca.newsspool1.arcor-online.net X-Trace: DXC=H3^65K8Z0bCA@P]\Dic==]BZ:afN4Fo<]lROoRA^YC2XCjHcbI\b@h]d3K4HEDNcfSJ;bb[EFCTGGVUmh?DLK[5LiR>kgB0?1mn1JQa5@ X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:5331 Date: 2009-03-27T09:54:29+01:00 List-Id: On Thu, 26 Mar 2009 14:57:13 -0700, Zachary Kline wrote: > I wrote for my own amusement a simple number-guessing game, which > works mostly as I intended. The problem comes when handling > exceptions. I can handle the case where a user typed a number too large > or too small: that's a Constraint_Error, and I just prompt again and > read another guess. The problem comes with Data_errors: if I try to > handle that case the same way, we get a seemingly infinite loop. A general advise is that when you read user input do not parse it as you read. That cannot work. Instead of that, read the whole line and then parse the string obtained, using Ada.Text_IO.Integer_IO (or something better): type Guess is new Integer; package Guess_IO is new Ada.Text_IO.Integer_IO (Guess); use Guess_IO; function Get_Guess return Guess is Result : Guess; begin loop Put ("Your guess:"); declare Line : constant String := Get_Line; Last : Positive; begin Get (Line, Result, Last); if Last = Line'Last then return Result; end if; Put_Line ("Unrecognized text after the number"); exception when Constraint_Error => Put_Line ("Out of range"); when Data_Error => Put_Line ("Not a number"); when End_Error => Put_Line ("Nothing found"); end; end loop; end Get_Guess; -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de