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=-0.9 required=3.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.6 Path: eternal-september.org!reader02.eternal-september.org!aioe.org!mRBVMlGoFUnDsRgMYRJiSw.user.46.165.242.75.POSTED!not-for-mail From: Richard Iswara Newsgroups: comp.lang.ada Subject: Re: Get_Line skip input Date: Fri, 27 Aug 2021 09:55:45 +0700 Organization: Aioe.org NNTP Server Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: gioia.aioe.org; logging-data="20939"; posting-host="mRBVMlGoFUnDsRgMYRJiSw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:62552 List-Id: On 26/08/2021 16.09, Jeffrey R. Carter wrote: > 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 > for entry> >> Keywords number 2 =  Enter Multiplier for keyword > for entry> > > 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; > Thank you Niklas and Jeffrey for the explanation and the better way to do an input message. Regards, Richard.