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=unavailable autolearn_force=no version=3.4.4 Path: Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.nethere.com!news.nethere.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 14 Feb 2014 01:05:24 -0600 Newsgroups: comp.lang.ada Subject: Re: Something I don't understand From: csampson@inetworld.net (Charles H. Sampson) Date: Thu, 13 Feb 2014 23:05:22 -0800 Message-ID: <1lh13qj.bxt340j5wwdoN%csampson@inetworld.net> References: <4a3e55f6-9f54-4084-9f37-96efd4b0d349@googlegroups.com> User-Agent: MacSOUP/2.8.3 (Mac OS X version 10.4.11 (PPC)) X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-xNlm7jz0owRu8JQByAnoi7NTqtHjyjtahcR1dS78ddZq2ft8J2+G6gRhsCqVwA2Dt7bgHw2r+FpsR/X!hgk4AyCNdAAyevEzXviquv7lYeevFoLKrTlL8hzsKKhfsousZYEEnLkbC8kypql6oelRTUvUslio!zpwSkPvSf6gIv344Y9jVsZGK2GrF X-Complaints-To: abuse@nethere.com X-DMCA-Complaints-To: abuse@nethere.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 4287 Xref: number.nntp.dca.giganews.com comp.lang.ada:184850 Date: 2014-02-13T23:05:22-08:00 List-Id: wrote: > On Thursday, February 13, 2014 3:57:58 PM UTC-8, Laurent wrote: > > Hi > > > > > > > > Doing an exercise from the book "Ada 95 Problem Solving and Program Design". > > > > I am supposed to make a robust function to read some values. It works so > > far but there is one thing I don't understand. > > > MaxName : CONSTANT Positive := 30; -- defined in other package > > SUBTYPE NameType IS String (1 .. MaxName); -- defined in other package > > > > Count : Natural RANGE 1.. MaxName; > > > > Name : LOOP > > BEGIN --exception handler block > > Ada.Text_IO.Put ("Name (1 - "); > > Ada.Integer_Text_IO.Put (Item => MaxName,Width => 1); > > Ada.Text_IO.Put(Item => " characters) >"); > > Ada.Text_IO.Get_Line (Item => S, Last => Count); > > Item.Name (1 .. Count) := S (1 .. Count); > > Ada.Text_IO.Skip_Line; > > > > EXIT; -- correct Name > > > > EXCEPTION > > WHEN Constraint_Error => > > Ada.Text_IO.Skip_Line; > > Ada.Text_IO.Put (Item => "Name too short/long!"); > > Ada.Text_IO.New_Line; > > > > END; -- exception handler > > > > END LOOP Name; > > > > > > > > > > > > So when I enter nothing as name I get a Constraint_Error which is > > handled. That's ok but when the name I enter is longer than 30 chars no > > exception. Is that normal and if yes/no why? > > Yes, because that's how the *procedure* Get_Line is defined. It stops > reading when it reaches the end of the string, or the end of the line. > (RM A.10.7(18-20)) > > If you want to check for input that is too long, you can use the > *function* Get_Line, which always reads to the end of the line: > > declare > Input : String := Ada.Text_IO.Get_Line; > begin > if Input'Length <= NameType'Length then > Item.Name (1 .. Input'Length) := Input; > --Ada.Text_IO.Skip_Line; --you shouldn't need this, Get_Line will > --automatically perform this > exit Name; -- correct name > else > Ada.Text_IO.Put_Line (Item => "Name too long!"); > -- combination of Put and New_Line > end if; > end; -- this ends the block starting with "declare" It should be mentioned that in the OP's version the extra characters aren't discarded. They're still there to be "gotten" on the next use of Get or Get_Line. As Mr. Beneschan hinted in his comment above, if you want to get rid of them you need to call Skip_Line. Just another example of Ada not doing things behind the scene. Charlie -- Nobody in this country got rich on his own. You built a factory--good. But you moved your goods on roads we all paid for. You hired workers we all paid to educate. So keep a big hunk of the money from your factory. But take a hunk and pay it forward. Elizabeth Warren (paraphrased)