comp.lang.ada
 help / color / mirror / Atom feed
From: anon@anon.org (anon)
Subject: Re: Newbie Question: Integer_IO an Data_error
Date: Fri, 27 Mar 2009 05:15:24 GMT
Date: 2009-03-27T05:15:24+00:00	[thread overview]
Message-ID: <MhZyl.84335$4m1.35924@bgtnsc05-news.ops.worldnet.att.net> (raw)
In-Reply-To: 873ad0ueva.fsf@babel.localdomain

You must use Skip_Line procedure and remove all Gets within the 
exception handlers!

with Ada.Text_IO ;
use Ada.Text_IO ;

function test return integer is -- just for the fun of it

  type Guess is new Integer ;
  package I_IO is new  Ada.Text_IO.Integer_IO ( Guess ) ;
  use I_IO ;

  procedure Get_Guess (Target : out Guess) is
    begin
      new_line ;
      Put ("Your guess: ");
      Get(Target);
    exception 
      when Constraint_Error => 
           Put_Line ("Please, numbers from one to a hundred only.");
      when Data_Error => 
           Put_Line ("Numbers only, please.");
      when others => 
           raise ;
    end Get_Guess;


  Temp : Guess := 0 ;

begin

  loop 
    begin 
      Get_Guess ( Temp ) ;
      Put_Line ("Done");
      exit when temp = 6 ;
    exception
      when others => 
          Put_Line ("Bad Data");
          return -7 ;
    end ;
    Skip_Line ;
  end loop ;

  return 0 ;

exception
  when others => 
    return -1 ;
end test ;




In <873ad0ueva.fsf@babel.localdomain>, Zachary Kline <kline.zachary@gmail.com> writes:
>Hi all,
>I'm quite new to the world of Ada, and currently trying to come to grips
>with it.
>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.
>I'm using Gnat 4.24 on Slackware Linux, if that matters.
>Code for the procedure in question is below:
>        procedure Get_Guess (Target : out Guess) is
>        begin
>           Put ("Your guess: ");
>           Get(Target);
>        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);
>                      when others => raise;
>        end Get_Guess;
>Any advice would be greatly appreciated.
>Best and thanks,
>Zack.
>-- 
>Love:
>When you like to think of someone on days that begin with a morning.




  parent reply	other threads:[~2009-03-27  5:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-26 21:57 Newbie Question: Integer_IO an Data_error Zachary Kline
2009-03-26 22:18 ` Tim Rowe
2009-03-26 23:57   ` Zachary Kline
2009-03-27  0:23     ` Tim Rowe
2009-03-27  0:03   ` Adam Beneschan
2009-03-27  0:19     ` Zachary Kline
2009-03-27  8:59   ` Jean-Pierre Rosen
2009-03-27  5:15 ` anon [this message]
2009-03-27  8:54 ` Dmitry A. Kazakov
replies disabled

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