From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-2.9 required=3.0 tests=BAYES_00,NICE_REPLY_A autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Get_Line skip input Date: Thu, 26 Aug 2021 11:09:41 +0200 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 26 Aug 2021 09:09:42 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="3e07e4055dbc54f2235c1cd0a2c50c8c"; logging-data="25190"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4h0JGmEymLFtqQfYEYKjukCfKie9nBWE=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 Cancel-Lock: sha1:JEeD92IkmaCmxlVrqzrEEhU+6MI= In-Reply-To: Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:62542 List-Id: On 8/26/21 7:36 AM, Richard Iswara wrote: > > So on the terminal it shows like this: > Keywords number 1 =  Enter Multiplier for keyword > Keywords number 2 =  Enter Multiplier for keyword I set Kw_Numbers to 3 and added an output loop after the input loop: for I in Key_Words'Range loop Ada.Text_IO.Put_Line (Item => I'Image & ' ' & Key_Words (I) (1 .. W_Len (I) ) & Multiplier (I)'Image); end loop; I am unable to duplicate this behavior: ~/Code$ ./kw_test Enter keywords less than 10 characters. Keywords number 1 = one Enter multiplier for keyword 7 Keywords number 2 = Enter multiplier for keyword 6 Keywords number 3 = Enter multiplier for keyword 3 1 one 7 2 6 3 3 As Holsti explained, this behavior is due to Ada.Integer_Text_IO.Get behaving as specified, leaving the rest of the line available to Get_Line: ~/Code$ ./kw_test Enter keywords less than 10 characters. Keywords number 1 = one Enter multiplier for keyword 7 Keywords number 2 = Enter multiplier for keyword 6 Keywords number 3 = Enter multiplier for keyword 3 1 one 7 2 6 3 3 ~/Code$ ./kw_test Enter keywords less than 10 characters. Keywords number 1 = one Enter multiplier for keyword 7two Keywords number 2 = Enter multiplier for keyword 6three Keywords number 3 = Enter multiplier for keyword 3 1 one 7 2 two 6 3 three 3 In general, I recommend obtaining a complete line and parsing it, rather than inputting values with Get: Get_Mult : loop Ada.Text_IO.Put (Item => "Enter multiplier for keyword "); One_Mult : declare Line : constant String := Ada.Text_IO.Get_Line; begin -- One_Mult Multiplier (I) := Integer'Value (Line); exit Get_Mult; exception -- One_Mult when others => Ada.Text_IO.Put_Line (Item => "Enter an non-negative integer"); end One_Mult; end loop Get_Mult; In general such code has to handle errors in the input. Users will input key words > the specified max and non-numeric multipliers. Error handling is simplified if one obtains complete lines (using the Get_Line function) and deals with them. Back in the Good Old Days (TM), I started off using FORTRAN 66 (it was always written FORTRAN because the keypunches only had capital letters), where the only data structure was the array. Things that would be an array of records in a decent language were represented as groups of parallel arrays. That seems to be what you're doing here. subtype KW_Name_Length is Integer range 0 .. 10; type Key_Word_Info (Length : KW_Name_Length := 0) is record Name : String (1 .. Length); Multiplier : Natural; end record; Num_KW : constant := 3; type KW_List is array (1 .. Num_KW) of Key_Word_Info; Key_Word : KW_List; -- Jeff Carter "Why don't you bore a hole in yourself and let the sap run out?" Horse Feathers 49