comp.lang.ada
 help / color / mirror / Atom feed
From: Robert Dewar <robert_dewar@my-deja.com>
Subject: Re: Newbie: Ada function like C++'s atoi?
Date: 2000/03/29
Date: 2000-03-29T00:00:00+00:00	[thread overview]
Message-ID: <8bsn4a$fhr$1@nnrp1.deja.com> (raw)
In-Reply-To: 38e17eab@news.siscom.net

In article <38e17eab@news.siscom.net>,
  "Keith C. McCready" <kmccready@agweb.com> wrote:

You pretty much solved your own problem

> I would like to read the user input as a character string

OK, that should be easy for you to find out how to do, since
you know about Text_IO, it is pretty easy to find the Get
that works on strings.

> convert and
> validate that the string is an integer value within the range
> of Percent_Type.

This is a little harder to find. The clue is that it is related
to a specific subtype, and such subtype related functions appear
as either operators or attributes (not functions, since clearly
there could not be a library function that knew about your
Percent_Type in advance).

So look through the attributes and you will find 'Value, and
low and behold, Percent_Type'Value is *exactly* what you are
looking for :-)

P.S. are you sure you want Percent_Type to be a subtype of
Integer, seems like a bad idea to me. Indeed a pretty general
rule in Ada is never to use the type Integer. Instead you
probably want

   type Percent_Type is range 0 .. 100;

This will prevent you from a mistake like

   X : Integer;
   Y : Percent_Type;

   --  Compute Y percent of X

   X := Y * X;

Now an ordinary multiplication here is complete nonsense. With
your declaration, it won't get caught, but with my declaration
above, this will be legal. You could even declare

   function "*" (A : Integer; B : Percent_Type) return Integer;

that does the "right" thing and allows the above assignment
resulting in the intended effect.




>
> *************************
> -- subtypes
> subtype Percent_Type is Integer range 0..100;
>
> -- variables:
> Percent    : Percent_Type;
>
> -- procedures:
> procedure Check_Percent ( Percent  : out Percent_Type ) is
>
> begin -- Check_Percent
>     Validation_Loop:
>       loop
>
>         Percent_Validation_Block:
>         begin
>             Integer_IO.Get ( Item => Percent );
>             exit Validation_Loop;
>
>             exception
>                  when CONSTRAINT_ERROR =>
>                       Text_IO.Put ( Item => "Valid range is 0
to 100" );
>                       Text_IO.New_Line;
>                       Text_IO.Skip_Line;
>
>                  when others =>
>                       Text_IO.Put ( Item => "Enter an integer
value of 0 to
> 100:  " );
>                       Text_IO.New_Line;
>                       Text_IO.Skip_Line;
>        end Percent_Validation_Block;
>
>   end loop Validation_Loop;
>
> end Check_Percent;
>
> ******************
>
> Thanks for any suggestions or comments,
>
> Keith C. McCready
>
> mccreak9708@uni.edu
>
> kmccready@agweb.com
>
>


Sent via Deja.com http://www.deja.com/
Before you buy.




      parent reply	other threads:[~2000-03-29  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-03-28  0:00 Newbie: Ada function like C++'s atoi? Keith C. McCready
2000-03-28  0:00 ` Vincent Marciante
2000-03-29  0:00 ` Robert Dewar [this message]
replies disabled

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