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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8dd8ee71ca4e5206 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-04 13:07:07 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!snoopy.risq.qc.ca!newsfeed.news2me.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread2.prod.itd.earthlink.net.POSTED!not-for-mail Message-ID: <3D9DF4DC.4000105@acm.org> From: Jeffrey Carter User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.0) Gecko/20020530 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Newbie question on Ada TExt_IO References: <93d4dcd4.0210031020.b0cca2b@posting.google.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Date: Fri, 04 Oct 2002 20:07:07 GMT NNTP-Posting-Host: 63.184.105.243 X-Complaints-To: abuse@earthlink.net X-Trace: newsread2.prod.itd.earthlink.net 1033762027 63.184.105.243 (Fri, 04 Oct 2002 13:07:07 PDT) NNTP-Posting-Date: Fri, 04 Oct 2002 13:07:07 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: archiver1.google.com comp.lang.ada:29525 Date: 2002-10-04T20:07:07+00:00 List-Id: Justin Birtwell wrote: > Accept entire line using Get_Line into string(1..256); > Check if first character is a digit > Check if digit is between 1 and 6 > error handling > > Here's the code: > > with Text_Io;use Text_Io; > with StringFunctions;use StringFunctions; > > procedure Test_IO_2 is > Input: String(1..256); > N:Integer; > Success:Boolean:=false; > Last:Integer; > begin > while success /=true loop Constructs like this frequently indicate a lack of understanding of what a Boolean value is. This could be while not Success loop That's kind of hard to understand when you go around the loop and when you stop. It's easier to understand if written loop exit when Success; However, Success is an unnecessary flag. It would be better to eliminate it, as shown below. > Put("PLease enter a number between 1 and 6"); > Get_Line(Item=>Input,Last=>Last); > if Is_Digit(Input(1..1)) then You could write if Ada.Characters.Handling.Is_Digit (Input (Input'First) ) then rather than writing your own function. > N:=Integer'Value(Input(1..1)); > if N>=1 and N<=6 then if N in 1 .. 6 then > Success:=True; > end if; > else > success:=false; > Put_Line("Invalid entry, try again."); > end if; > end loop; > Put_Line("Thank you"); > exception > when others=> > Put_Line("Error, Invalid data."); > end; If the user enters "60" you're going to interpret it as 6, and if " 1" will be an error. Is that really what you want? Doing subtype Valid_Number is Integer range 1 .. 6; N : Valid_Number; ... N := Integer'Value (Input (Input'First .. Last) ); will reject "60" (not in range) and interpret " 1" as 1. You can write what you have as: subtype Valid_Number is Integer range 1 .. 6; N : Valid_Number; ... Get_Number : loop -- Get_Line Check : begin N := Integer'Value (Input (Input'First .. Last) ); exit Get_Number; exception -- Check when others => -- Error message end Check; end loop Get_Number; which seems a lot shorter and clearer (though admittedly some of the simplification comes from changing how the input is interpreted). It may take you a little while to become comfortable with Ada to the point that you come up with things like the above rather than the more complicated version you have, so don't get discouraged. > > Is_Digit a hack of a procedure that compares the 1 character string to all > 10 digits characters. I really have a lot to learn. What's giving me the > hardest time is working within the confines of strict/strong typing. The > Ada.Characters.Handling package already has an Is_Digit function, but it's > expecting a character not a single item String array! How can I convert > from one into the other? I have the distinct feelling like I'm reinventing > the wheel. Aren't packages aready made to do alot of this > converting/validation? If so where are they? How can one access them? Type String is an array of type Character, so Input (N) is of type Character. See my suggestion above. > Does the RM list all the packages provided by Ada or are there more to be > discovered? The ARM lists all the standard packages that come with all compilers. There are lots of other packages available. www.adapower.com is probably a good place to start if you want to find others. -- Jeff Carter "I wave my private parts at your aunties." Monty Python & the Holy Grail