From: Preben Randhol <randhol+news@pvv.org>
Subject: Re: Newbie question on Ada TExt_IO
Date: Thu, 3 Oct 2002 19:43:32 +0000 (UTC)
Date: 2002-10-03T19:43:32+00:00 [thread overview]
Message-ID: <slrnapp7f6.45u.randhol+news@kiuk0156.chembio.ntnu.no> (raw)
In-Reply-To: 93d4dcd4.0210031020.b0cca2b@posting.google.com
On 3 Oct 2002 11:20:24 -0700, Justin wrote:
> 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?
No I would have used exceptions here. I find it difficult to see how you
can avoid it.
I recommend that you look at http://www.it.bton.ac.uk/staff/je/adacraft/ because
it has a lot of nice examples to start with.
Here is my implementation:
---------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
procedure Feedback
is
Input : String(1..80);
Last : Integer;
begin
-- This example will loop until you give a correct number
loop
Put ("Please enter a number between 1 and 6 : ");
Get_Line (Item => Input, Last => Last);
-- I now choose a block because then I can
-- put a exception handler inside so that you will
-- be asked for a number until you give a correct
-- number
declare
-- Making a type that is from 1 to 6. If the character
-- you convert isn't in the range 1..6 then Constraint_Error
-- will be raised and we can handle it.
type Input_Number_Type is range 1..6;
Number : Input_Number_Type;
begin
-- we convert Input (1..1) to our Input_Number_Type
Number := Input_Number_Type'Value (Input (1..1));
Put_Line ("Thank you!");
-- OK conversion went well. (If it hadn't the program would
-- jump to exception below before instead of continuing)
exit;
-- Jumping out of the loop.
exception
when Constraint_Error =>
Put_Line ("Wrong number!");
end;
end loop;
end Feedback;
Preben
--
Ada95 is good for you.
http://libre.act-europe.fr/Software_Matters/02-C_pitfalls.pdf
next prev parent reply other threads:[~2002-10-03 19:43 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-10-03 18:20 Newbie question on Ada TExt_IO Justin
2002-10-03 18:50 ` Matthew Heaney
2002-10-03 19:05 ` Jeffrey Carter
2002-10-03 19:35 ` David C. Hoos
2002-10-03 19:35 ` tmoran
2002-10-03 19:43 ` Preben Randhol [this message]
2002-10-03 19:55 ` Matthew Heaney
2002-10-03 20:07 ` Preben Randhol
2002-10-04 2:42 ` SteveD
2002-10-04 17:49 ` Justin Birtwell
2002-10-04 18:00 ` David C. Hoos
2002-10-04 18:04 ` Preben Randhol
2002-10-04 18:00 ` Preben Randhol
2002-10-04 18:02 ` Preben Randhol
2002-10-04 18:34 ` tmoran
2002-10-04 17:34 ` Justin Birtwell
2002-10-04 17:58 ` Preben Randhol
2002-10-04 18:13 ` tmoran
2002-10-04 20:07 ` Jeffrey Carter
2002-10-07 8:26 ` Fraser Wilson
2002-10-07 19:44 ` Jeffrey Carter
2002-10-05 2:43 ` SteveD
2002-10-05 5:25 ` tmoran
-- strict thread matches above, loose matches on Subject: below --
2002-10-07 6:01 Grein, Christoph
2002-10-07 8:27 Grein, Christoph
2002-10-07 11:48 ` Fraser Wilson
2002-10-07 12:46 ` Robert A Duff
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox