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: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.glorb.com!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!ottix-news.ottix.net!newsswitch.lcs.mit.edu!nntp.TheWorld.com!.POSTED!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Something I don't understand Date: Sat, 15 Feb 2014 20:39:34 -0500 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <4a3e55f6-9f54-4084-9f37-96efd4b0d349@googlegroups.com> <0b358700-871b-4603-addd-65e07c7d59e5@googlegroups.com> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls7.std.com 1392514776 3603 192.74.137.71 (16 Feb 2014 01:39:36 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Sun, 16 Feb 2014 01:39:36 +0000 (UTC) User-Agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.3 (irix) Cancel-Lock: sha1:Pcr6wv8iO13LzM3kuCneqBD+NZ4= Xref: news.eternal-september.org comp.lang.ada:18598 Date: 2014-02-15T20:39:34-05:00 List-Id: Laurent writes: > Now it works as I think it should. Good. FWIW, Text_IO is a poor design, and I don't blame you for having trouble with it! Some stylistic comments on the code below: Exceptions are for dealing with cases where one piece of code detects a (possible) error, and another piece of code decides whether it really is an error, and how to deal with it. That's not the case here, so should be written without any exceptions. No need for a loop name. You should use typical style, so other Ada programmers can read your code more easily: lower case keywords, Max_Name instead of MaxName. (Or Max_Name_Length?) Space after "--". S should declared "constant". You should enable warnings in GNAT that detect this mistake. Why should there be a limit on the length of the name? Maybe you have a good reason, but usually such arbitrary limits are a bad idea. > 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) >"); > > DECLARE > S : String := Ada.Text_IO.Get_Line; > > BEGIN > IF S'Length = 0 THEN > RAISE Name_Too_Short; > ELSIF S'Length <= MaxName THEN > Item.Name (1 .. S'Length) := S; > ELSE RAISE Name_Too_Long; > END IF; > > EXIT; -- correct Name > > EXCEPTION > > WHEN Name_Too_Short => > Ada.Text_IO.Skip_Line; > Ada.Text_IO.Put (Item => "Name too short!"); > Ada.Text_IO.New_Line; > WHEN Name_Too_Long => > Ada.Text_IO.Skip_Line; > Ada.Text_IO.Put_Line (Item => "Name too long!"); > > END; -- exception handler > END; > END LOOP Name;