From: "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Get_Line skip input
Date: Thu, 26 Aug 2021 11:09:41 +0200 [thread overview]
Message-ID: <sg7lom$oj6$1@dont-email.me> (raw)
In-Reply-To: <sg798l$3r0$1@gioia.aioe.org>
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 <here cursor wait for entry>
> Keywords number 2 = Enter Multiplier for keyword <here cursor wait 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;
--
Jeff Carter
"Why don't you bore a hole in yourself and let the sap run out?"
Horse Feathers
49
next prev parent reply other threads:[~2021-08-26 9:09 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-26 5:36 Get_Line skip input Richard Iswara
2021-08-26 7:56 ` Niklas Holsti
2021-08-26 9:09 ` Jeffrey R. Carter [this message]
2021-08-27 2:55 ` Richard Iswara
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox