From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: * X-Spam-Status: No, score=1.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ec8527ae307e5e17 X-Google-Attributes: gid103376,public From: Robert Dewar Subject: Re: Newbie: Ada function like C++'s atoi? Date: 2000/03/29 Message-ID: <8bsn4a$fhr$1@nnrp1.deja.com>#1/1 X-Deja-AN: 603800149 References: <38e17eab@news.siscom.net> X-Http-Proxy: 1.0 x33.deja.com:80 (Squid/1.1.22) for client 205.232.38.14 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Wed Mar 29 10:49:49 2000 GMT X-MyDeja-Info: XMYDJUIDrobert_dewar Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.61 [en] (OS/2; I) Date: 2000-03-29T00:00:00+00:00 List-Id: In article <38e17eab@news.siscom.net>, "Keith C. McCready" 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.