From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,49f7dd1bad1910ff X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-13 18:29:19 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread4.news.pas.earthlink.net.POSTED!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Problems with Ada.Text_IO and strings. References: <8336eb21.0310131423.7410e1ec@posting.google.com> In-Reply-To: <8336eb21.0310131423.7410e1ec@posting.google.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Date: Tue, 14 Oct 2003 01:29:18 GMT NNTP-Posting-Host: 63.184.17.143 X-Complaints-To: abuse@earthlink.net X-Trace: newsread4.news.pas.earthlink.net 1066094958 63.184.17.143 (Mon, 13 Oct 2003 18:29:18 PDT) NNTP-Posting-Date: Mon, 13 Oct 2003 18:29:18 PDT Xref: archiver1.google.com comp.lang.ada:797 Date: 2003-10-14T01:29:18+00:00 List-Id: 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