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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: After typing input of Integer it SKIPS getting the inputs of String. Date: Sat, 11 Jul 2015 11:05:29 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <4ab70bc8-b25f-4dc4-88d9-d7021c2bed30@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Sat, 11 Jul 2015 18:04:05 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="a3855fbfe1a666be9aefba0563039ed5"; logging-data="11294"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18MyzdFA18UGAzXqG2Y2JyowsOTEsvgZvo=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 In-Reply-To: <4ab70bc8-b25f-4dc4-88d9-d7021c2bed30@googlegroups.com> Cancel-Lock: sha1:N+sZOXMlx7xscBoo5koJb2byIJg= Xref: news.eternal-september.org comp.lang.ada:26781 Date: 2015-07-11T11:05:29-07:00 List-Id: On 07/11/2015 08:14 AM, Trish Cayetano wrote: > Put_Line("Enter Integer"); > Get(inputNmbr,1); > Put_Line("Enter String"); > Get_Line(inputText,StringNatural); > Put_Line("==================="); > Put("Input for Integer: "); > Put(inputNmbr,1); > Put_Line(""); > Put_Line("Input for String: "); > Put_Line(inputText(1..StringNatural)); > > Enter Integer > 2 > Enter String > =================== > Input for Integer: 2 > Input for String: You don't say exactly what characters you input to get this, but I'm guessing you typed 2 A lot of people either don't read or don't understand the description of Integer_IO.Get, which is at ARM A.10.8 (8-10) http://www.adaic.org/resources/add_content/standards/12rm/html/RM-A-10-8.html If you had, you'd expect exactly what you got. It reads 8 If the value of the parameter Width is zero, skips any leading blanks, line terminators, or page terminators, then reads a plus sign if present or (for a signed type only) a minus sign if present, then reads the longest possible sequence of characters matching the syntax of a numeric literal without a point. If a nonzero value of Width is supplied, then exactly Width characters are input, or the characters (possibly none) up to a line terminator, whichever comes first; any skipped leading blanks are included in the count. 9 Returns, in the parameter Item, the value of type Num that corresponds to the sequence input. 10/3 The exception Data_Error is propagated if the sequence of characters read does not form a legal integer literal or if the value obtained is not of the subtype Num. Note that it says nothing about skipping a line terminator. So if you wanted the String to be "Hello", you'd have to type 2Hello The behavior of Get is especially interesting if you want to handle input errors: Valid_Integer : loop Ada.Text_IO.Put (Item => "Enter an integer: "); Handle_Error : begin Ada.Integer_Text_IO.Get (Item => I); exit Valid_Integer; exception -- Handle_Error when others => Ada.Text_IO.Put_Line (Item => "Invalid input. Try again."); end Handle_Error; end loop Valid_Integer; If the user accidentally types 'q' for the integer, this is an infinite loop, because nothing ever reads the 'q'. For this reason, many of us like to read a line into a String using Get_Line and parse it using the Get with a "From : in String" parameter. Valid_Integer : loop Ada.Text_IO.Put (Item => "Enter an integer: "); Handle_Error : declare Line : constant String := Ada.Text_IO.Get_Line; Last : Natural; begin -- Handle_Error Ada.Integer_Text_IO.Get (From => Line, Item => I, Last => Last); exit Valid_Integer; exception -- Handle_Error when others => Ada.Text_IO.Put_Line (Item => "Invalid input. Try again."); end Handle_Error; end loop Valid_Integer; -- Jeff Carter "How'd you like to hide the egg and gurgitate a few saucers of mocha java?" Never Give a Sucker an Even Break 101