comp.lang.ada
 help / color / mirror / Atom feed
From: "SteveD" <nospam_steved94@attbi.com>
Subject: Re: Newbie question on Ada TExt_IO
Date: Fri, 04 Oct 2002 02:42:41 GMT
Date: 2002-10-04T02:42:41+00:00	[thread overview]
Message-ID: <Be7n9.27185$FO4.6440@sccrnsc03> (raw)
In-Reply-To: 93d4dcd4.0210031020.b0cca2b@posting.google.com

"Justin" <jbirtwell@yahoo.com> 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





  parent reply	other threads:[~2002-10-04  2:42 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
2002-10-03 19:55   ` Matthew Heaney
2002-10-03 20:07     ` Preben Randhol
2002-10-04  2:42 ` SteveD [this message]
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