comp.lang.ada
 help / color / mirror / Atom feed
From: adambeneschan@gmail.com
Subject: Re: Something I don't understand
Date: Thu, 13 Feb 2014 16:18:15 -0800 (PST)
Date: 2014-02-13T16:18:15-08:00	[thread overview]
Message-ID: <ad687a22-84c1-4c7d-bf8d-023fda30d849@googlegroups.com> (raw)
In-Reply-To: <4a3e55f6-9f54-4084-9f37-96efd4b0d349@googlegroups.com>

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"

                            -- Adam

  reply	other threads:[~2014-02-14  0:18 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-13 23:57 Something I don't understand Laurent
2014-02-14  0:18 ` adambeneschan [this message]
2014-02-14  7:05   ` Charles H. Sampson
2014-02-15 15:27   ` Laurent
2014-02-15 19:10     ` Laurent
2014-02-15 20:05       ` Niklas Holsti
2014-02-15 21:16         ` Laurent
2014-02-15 21:40       ` Jeffrey Carter
2014-02-16  1:39       ` Robert A Duff
2014-02-16  9:08         ` Text_IO, was: " Simon Clubley
2014-02-16  9:43           ` Dmitry A. Kazakov
2014-02-16 16:57             ` Dennis Lee Bieber
2014-02-16 16:17           ` Robert A Duff
2014-02-17 12:52             ` Simon Clubley
2014-02-17 15:32               ` G.B.
2014-02-17 15:35                 ` G.B.
2014-02-17 17:34                 ` Mike H
2014-02-17 16:59               ` Niklas Holsti
2014-02-17 17:17                 ` Dmitry A. Kazakov
2014-02-17 17:42                   ` Niklas Holsti
2014-02-17 19:55                     ` Dmitry A. Kazakov
2014-02-18  7:14                       ` Niklas Holsti
2014-02-18  8:40                         ` Dmitry A. Kazakov
2014-02-18  9:00                           ` Niklas Holsti
2014-02-18  9:31                             ` Dmitry A. Kazakov
2014-02-19  8:36                               ` Niklas Holsti
2014-02-19  9:40                                 ` Dmitry A. Kazakov
2014-02-19 13:20                                   ` Niklas Holsti
2014-02-19 14:13                                     ` Dmitry A. Kazakov
2014-02-19 15:37                                       ` Georg Bauhaus
2014-02-19 16:32                                         ` Laurent
2014-02-19 17:46                                           ` Simon Clubley
2014-02-20  2:39                                         ` Dennis Lee Bieber
2014-02-20 11:44                                           ` G.B.
2014-02-19 21:45                                       ` Niklas Holsti
2014-02-20  9:52                                         ` Dmitry A. Kazakov
2014-02-20 18:19                                           ` Niklas Holsti
2014-02-19 15:06                                     ` Robert A Duff
2014-02-19 17:03                                       ` Niklas Holsti
2014-02-19 22:30                                         ` Robert A Duff
2014-02-17 18:13                 ` Simon Clubley
2014-02-17 20:09                   ` Dmitry A. Kazakov
2014-02-18  7:50                     ` Georg Bauhaus
2014-02-18  8:28                       ` Dmitry A. Kazakov
2014-02-17 20:22                   ` Niklas Holsti
2014-02-18  0:50                     ` Simon Clubley
2014-02-18  6:56                       ` Niklas Holsti
2014-02-18  8:04                         ` Georg Bauhaus
2014-02-19 22:01                     ` Robert A Duff
2014-02-20  8:25                       ` Dmitry A. Kazakov
2014-02-20 15:54                         ` Robert A Duff
2014-02-20 17:54                           ` Dmitry A. Kazakov
2014-02-20 20:45                       ` Niklas Holsti
2014-02-19 21:52                   ` Robert A Duff
2014-02-20  0:50                     ` Simon Clubley
2014-02-19 21:46                 ` Robert A Duff
2014-02-20  0:09                   ` Jeffrey Carter
2014-02-20  1:09                     ` Simon Clubley
2014-02-20  7:06                       ` Niklas Holsti
2014-02-20 13:05                         ` Simon Clubley
2014-02-20 11:51                       ` G.B.
2014-02-20 12:53                         ` Simon Clubley
2014-02-21 11:50                       ` Brian Drummond
2014-02-23 21:37                         ` AdaMagica
2014-02-23 23:23                           ` Bill Findlay
2014-02-24  4:29                           ` AdaMagica
2014-02-24 12:22                           ` Brian Drummond
2014-02-24 19:03                             ` AdaMagica
2014-02-20 20:02                   ` Niklas Holsti
2014-02-19 21:15               ` Robert A Duff
2014-02-19 22:01                 ` Simon Clubley
2014-02-16 14:50         ` Mike H
2014-02-17 16:09         ` Laurent
2014-02-17 17:42           ` Mike H
2014-02-18  1:05             ` Dennis Lee Bieber
2014-02-17 22:31           ` Jeffrey Carter
2014-02-19 12:51             ` Laurent
replies disabled

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