* Newbie: Ada function like C++'s atoi?
@ 2000-03-28 0:00 Keith C. McCready
2000-03-28 0:00 ` Vincent Marciante
2000-03-29 0:00 ` Robert Dewar
0 siblings, 2 replies; 3+ messages in thread
From: Keith C. McCready @ 2000-03-28 0:00 UTC (permalink / raw)
Greetings,
I am a college student taking an introductory Ada programming class who is
familiar with C/C++. Looking for Ada function similar to atoi. Purpose: I
want the following to raise an exception when the user enters a floating
point value. Right now, if the user enters a value of 3.1415, the value
received by the Get is 3.
I would like to read the user input as a character string, convert and
validate that the string is an integer value within the range of
Percent_Type.
*************************
-- 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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Newbie: Ada function like C++'s atoi?
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
1 sibling, 0 replies; 3+ messages in thread
From: Vincent Marciante @ 2000-03-28 0:00 UTC (permalink / raw)
You are using Integer_IO's get.
One of the other standard ..._IO packages provides the facilities that
you want.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Newbie: Ada function like C++'s atoi?
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
1 sibling, 0 replies; 3+ messages in thread
From: Robert Dewar @ 2000-03-29 0:00 UTC (permalink / raw)
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2000-03-29 0:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox