From: Jeffrey Carter <spam@spam.com>
Subject: Re: Problems with Ada.Text_IO and strings.
Date: Tue, 14 Oct 2003 01:29:18 GMT
Date: 2003-10-14T01:29:18+00:00 [thread overview]
Message-ID: <OjIib.135$7a4.122@newsread4.news.pas.earthlink.net> (raw)
In-Reply-To: <8336eb21.0310131423.7410e1ec@posting.google.com>
Bleakcabal wrote:
> Hi, im new to Ada and I am writing my first program trying to "get"
> Ada.
>
> My small program ask for input from the user. I read it using :
>
> Ada.Text_IO.Get_Line (MyType.Name, NameLength);
>
> This data is then stored inside an array of a type I have declared :
>
> type MyType is record
> name : string(1..20);
> adress : string(1..40);
> telephone : natural;
> end record;
>
> Later in the program I output this information using :
> Ada.Text_IO.Put (MyType.name);
>
> My problem is this : when I output the name, part of the string is
> filled with "garbage" characters. If I fill the string with 20
> characters I get no garbage. But when I write a name that's about 10
> characters long, I get about 10 characters of garbage.
Mytype.Name is 20 characters long. It is always 20 characters long,
regardless of what those characters are. "Put (X.Name);" (assuming "X :
Mytype;"; what you have is illegal) outputs all 20 characters. If you
haven't assigned to all 20 characters, then some of them contain junk.
Your problem is that you are not storing the length that you get from
Get_Line. (Technically, this is the index of the last position filled,
not the length. This is the same as the length in your case because the
lower bound is one, but there's no requirement that it be one; it could
just as well be 2 or 17.)
Your choices include storing the length and using it to slice the
string, or using Ada.Strings.[Un]Bounded.
You'll probably find another kind of unexpected behavior if you enter
more than 20 characters for the name.
You need to read the documentation for Get_Line and be sure you
understand what it does for a line with fewer characters than the
string, the same number of characters as the string, and more characters
than the string.
You also have to understand that String in Ada is simply a
one-dimensional array of characters, no different than any other array.
A String always has the same number of characters, regardless of what
you put in it.
--
Jeff Carter
"When danger reared its ugly head, he bravely
turned his tail and fled."
Monty Python and the Holy Grail
60
next prev parent reply other threads:[~2003-10-14 1:29 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 [this message]
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
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