comp.lang.ada
 help / color / mirror / Atom feed
From: gisle@apal.ii.uib.no (Gisle S�lensminde)
Subject: Re: raised CONSTRAINT_ERROR
Date: 2000/07/23
Date: 2000-07-23T00:00:00+00:00	[thread overview]
Message-ID: <slrn8nlpu2.6dc.gisle@apal.ii.uib.no> (raw)
In-Reply-To: 86lmytp5i0.fsf@book.mteege.de

In article <86lmytp5i0.fsf@book.mteege.de>, Matthias Teege wrote:
>
>Moin,
>
>I made my first steps in Ada using GNATS under FreeBSD and
>now I have a problem that I didnt understand. The followig
>code gives me a raised CONSTRAINT_ERROR (in Line:
>StrArg := Ada.Command_Line.Argument(I)) and I didnt know
>why.

In the first version a string of length exactly 10 is used. In
the other version a generic string (allocated on stack) is
used. In Ada it's not allowed to assign strings of different
length, and the 

If you write on command line: 

% cldemo 0123456789 9876543210 abcdefghij 

The program will not give constraint error because all strings
is of the correct size. The string type have fixed size. If you 
want to handle dynamic sized strings use unbounded strings,
which is there to handle such cases. 

Another way is to use a block:

declare 
  Arg : String := Ada.Command_Line.Argument_Count;
begin
  put_line(Arg);
end;

This way a local variable arg (with desired size) is introduced.


>
>with Ada.Command_Line; use Ada.Command_Line;
>with GNAT.IO; use GNAT.IO;
>
>procedure Cldemo is
>
>Argcount: Natural;
>StrArg : String (1 .. 10);
>I: Positive := 1;
>
>begin
>   Argcount := Ada.Command_Line.Argument_Count;
>
>Schleife:
>   while I <= Argcount loop
>      StrArg := Ada.Command_Line.Argument(I);
>      Put_Line(StrArg);
>      I := I + 1;
>   end loop Schleife;
>
>end Cldemo;
>
>If I change the Code to
>
>with Ada.Command_Line; use Ada.Command_Line;
>with GNAT.IO; use GNAT.IO;
>
>procedure Cldemo is
>
>Argcount: Natural;
>I: Positive := 1;
>
>begin
>   Argcount := Ada.Command_Line.Argument_Count;
>
>Schleife:
>   while I <= Argcount loop
>      Put_Line(Ada.Command_Line.Argument(I));
>      I := I + 1;
>   end loop Schleife;
>
>end Cldemo;
>
>all works perfect. What is the main difference between
>both versions?
>
>Thanks for any hints
>Matthias
>


-- 
--
Gisle S�lensminde ( gisle@ii.uib.no )   

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going
to land, and it could be dangerous sitting under them as they fly
overhead. (RFC 1925)




  reply	other threads:[~2000-07-23  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-07-23  0:00 raised CONSTRAINT_ERROR Matthias Teege
2000-07-23  0:00 ` Gisle S�lensminde [this message]
2000-07-24  0:00   ` Gisle S�lensminde
2000-07-24  0:00 ` Ted Dennison
2000-07-24  0:00 ` Pascal Obry
replies disabled

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