comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Problems with Ada.Text_IO and strings.
Date: Thu, 16 Oct 2003 01:19:48 GMT
Date: 2003-10-16T01:19:48+00:00	[thread overview]
Message-ID: <Ummjb.2535$s93.1800@newsread3.news.pas.earthlink.net> (raw)
In-Reply-To: <m3smlui2jw.fsf@insalien.org>

Ludovic Brenta wrote:

> I'm not going to post working code and do your homework for you, but
> here are hints you may find useful.
> 
> Hint 1:
> 
> declare
>    S : String (1 .. 20);
>    Last : Natural;
> begin
>    Ada.Text_IO.Get_Line (S, Last); -- User's input is S (S'First .. Last)
> end;
> 
> Hint 2: if Last = S'Last, then you will want to call
> Ada.Text_IO.Get_Line again to read the rest of the input, and
> concatenate this to S.
> 
> Hint 3: recursive calls are your friends.

I don't think this is what the OP is looking for (a Get_Line function).

One way to deal with this is to read into a really long String using 
Get_Line, then use Ada.Strings.Fixed.Move to transfer the value to the 
Name variable. It's unlikely that a user will type in 100 characters for 
a name; 1000 practically guarantees that Get_Line will get the entire line.

Another is to write Get_Line yourself, not because you need to, but 
because it helps you understand what Get_Line is doing:

procedure Get_Line (Item : out String; Last : out Natural) is
begin
    Last := Item'First - 1;

    Read : for I in Item'range loop
       if End_Of_Line then
          Skip_Line;

          return;
       end if;

       Get (Item (I) );
       Last := I;
    end loop Read;
end Get_Line;

The important point is that Skip_Line is only called inside the loop. 
The loop exits when Item is completely filled; Skip_Line is not called. 
If the loop exits, Last = Item'Last. Thus, if on return from Get_Line, 
Last = Item'Last, then there is at least an unprocessed line terminator 
in the buffer, possibly preceded by some unprocessed characters. You can 
get rid of these by calling Skip_Line yourself.

You can also write a procedure to deal with all of this:

procedure Get_Whole_Line (Item : out String; Last : out Natural) is
begin
    Get_Line (Item, Last);

    if Last = Item'Last then
       Skip_Line;
    end if;
end Get_Whole_Line;

-- 
Jeff Carter
"Go and boil your bottoms."
Monty Python & the Holy Grail
01




  reply	other threads:[~2003-10-16  1:19 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-13 22:23 Problems with Ada.Text_IO and strings Bleakcabal
2003-10-13 23:51 ` Larry Hazel
2003-10-14  0:06 ` Chad R. Meiners
2003-10-14 12:32   ` Bleakcabal
2003-10-14 15:03     ` Robert I. Eachus
2003-10-14 15:16       ` Stephane Richard
2003-10-14 20:16         ` Jeffrey Carter
2003-10-14  1:29 ` Jeffrey Carter
2003-10-14 12:39   ` Bleakcabal
2003-10-14 12:57     ` sk
2003-10-14 14:14     ` Problems with Ada.Text_IO and strings. (wants EOL character) Larry Kilgallen
2003-10-14 16:20       ` Stephen Leake
2003-10-14 16:45         ` Stephane Richard
2003-10-14 20:19     ` Problems with Ada.Text_IO and strings Jeffrey Carter
2003-10-14 12:33 ` Bleakcabal
2003-10-15  6:25 ` CheGueVerra
2003-10-15 14:41   ` Martin Krischik
2003-10-15 19:50     ` CheGueVerra
2003-10-15 22:00       ` Ludovic Brenta
2003-10-16  1:19         ` Jeffrey Carter [this message]
2003-10-15 23:39       ` Chad R. Meiners
2003-10-19  7:36         ` Martin Krischik
2003-10-19 19:24           ` Chad R. Meiners
2003-10-15 17:23   ` Marius Amado Alves
2003-10-16  2:29   ` Steve
2003-10-16  5:51     ` CheGueVerra
2003-10-16  9:51       ` CheGueVerra
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox