comp.lang.ada
 help / color / mirror / Atom feed
* Constraint_Error
@ 1987-04-10 21:47 keith
  0 siblings, 0 replies; 10+ messages in thread
From: keith @ 1987-04-10 21:47 UTC (permalink / raw)



Well, if you want me to read the LRM to you:

3.5.4:12 "... in which case an attempt to assign the result to a variable
          of the integer subtype raises the exception CONSTRAINT_ERROR."

So from your original incorrect Ada "type x is range 0..100"; either
possible translation "subtype x is range 0..100" or
"type x is new integer range 0..100", the language standard clearly states
that assigning a value outside of the specified range (check 3.3.2:9) will
raise CONSTRAINT_ERROR.

Sorry for my earlier post.  I assumed you had read the manual.

Keith

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Constraint error
@ 1999-04-07  0:00 Nirmala Bhupathi
  1999-04-07  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 10+ messages in thread
From: Nirmala Bhupathi @ 1999-04-07  0:00 UTC (permalink / raw)


Hi,

I am getting a constraint error with this program.
Is it behaving right?


with Ada.Text_IO;
with Interfaces;

procedure Show is

  type Local_T is mod 32;

  type Alpha_T   is new Local_T;
  type Bravo_T   is new Interfaces.Unsigned_32;
  type Charlie_T is digits 6;

  package Alpha_IO   is new Ada.Text_IO.Modular_IO(Alpha_T);
  package Bravo_IO   is new Ada.Text_IO.Modular_IO(Bravo_T);
  package Charlie_IO is new Ada.Text_IO.Float_IO(Charlie_T);

  A : Alpha_T;
  B : Bravo_T;
  C : Charlie_T;

begin

  Ada.Text_IO.Put ("Enter: ");

  Charlie_IO.Get(C);

  A := Alpha_T(C);

  Alpha_IO.Put(A);

  Ada.Text_IO.New_Line;

  B := Bravo_T(C);

  Bravo_IO.Put(B);

  Ada.Text_IO.New_Line;

end Show;




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Constraint error
  1999-04-07  0:00 Constraint error Nirmala Bhupathi
@ 1999-04-07  0:00 ` Matthew Heaney
  1999-04-07  0:00   ` Nirmala Bhupathi
  0 siblings, 1 reply; 10+ messages in thread
From: Matthew Heaney @ 1999-04-07  0:00 UTC (permalink / raw)


Nirmala Bhupathi <nbhupath@rational.com> writes:

> I am getting a constraint error with this program.
> Is it behaving right?

Works OK for me.  

What value did you enter to get CE?  Was it negative?  (If so, then that
would explain the error.)



> with Ada.Text_IO;
> with Interfaces;
> 
> procedure Show is
> 
>   type Local_T is mod 32;
> 
>   type Alpha_T   is new Local_T;
>   type Bravo_T   is new Interfaces.Unsigned_32;
>   type Charlie_T is digits 6;
> 
>   package Alpha_IO   is new Ada.Text_IO.Modular_IO(Alpha_T);
>   package Bravo_IO   is new Ada.Text_IO.Modular_IO(Bravo_T);
>   package Charlie_IO is new Ada.Text_IO.Float_IO(Charlie_T);
> 
>   A : Alpha_T;
>   B : Bravo_T;
>   C : Charlie_T;
> 
> begin
> 
>   Ada.Text_IO.Put ("Enter: ");
> 
>   Charlie_IO.Get(C);
> 
>   A := Alpha_T(C);
> 
>   Alpha_IO.Put(A);
> 
>   Ada.Text_IO.New_Line;
> 
>   B := Bravo_T(C);
> 
>   Bravo_IO.Put(B);
> 
>   Ada.Text_IO.New_Line;
> 
> end Show;




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Constraint error
  1999-04-07  0:00 ` Matthew Heaney
@ 1999-04-07  0:00   ` Nirmala Bhupathi
  1999-04-08  0:00     ` Matthew Heaney
  0 siblings, 1 reply; 10+ messages in thread
From: Nirmala Bhupathi @ 1999-04-07  0:00 UTC (permalink / raw)
  To: Matthew Heaney

I entered 3.2.
Then I get 3 (Alplha_Io.Put(A))
and Constraint Error




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Constraint error
  1999-04-07  0:00   ` Nirmala Bhupathi
@ 1999-04-08  0:00     ` Matthew Heaney
  0 siblings, 0 replies; 10+ messages in thread
From: Matthew Heaney @ 1999-04-08  0:00 UTC (permalink / raw)


3.2 works OK for me.




Nirmala Bhupathi <nbhupath@rational.com> writes:

> I entered 3.2.
> Then I get 3 (Alplha_Io.Put(A))
> and Constraint Error




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Constraint Error
@ 2004-09-29 23:24 Rick Santa-Cruz
  2004-09-30  0:42 ` Jeffrey Carter
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Rick Santa-Cruz @ 2004-09-29 23:24 UTC (permalink / raw)


Hi,

how can I avoid the Constraint Error, when executing the following code:

with Messages;

procedure Test_Messages is
 M1: Messages.Message;
 M2: Messages.Message;

begin
 m1:=Messages.Create("This is a test");
 m2:=Messages.Create("This is a second test");
end Test_Messages;
----------------------------
 package Messages is
 type Message is tagged private;
 subtype String_Txt is String(1..20);

 function Create(Str: String) return Message;

 private
  type Message is tagged
   record
    Text: String_Txt;
   end record;
end Messages;
------------------------------
package body Messages is
 function Create(Str: String) return Message is
  M: Message;
 begin
  M.Text := Str;
  return M;
 end Create;
end Messages;

The Constraint Error occurs in the Create-Function.

Thanks in advance,
Rick 





^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Constraint Error
  2004-09-29 23:24 Constraint Error Rick Santa-Cruz
@ 2004-09-30  0:42 ` Jeffrey Carter
  2004-09-30  0:42 ` Stephen Leake
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: Jeffrey Carter @ 2004-09-30  0:42 UTC (permalink / raw)


Rick Santa-Cruz wrote:
> 
> how can I avoid the Constraint Error, when executing the following code:

Don't violate the constraint.

>  package Messages is
>  type Message is tagged private;
>  subtype String_Txt is String(1..20);

Objects of this subtype always have exactly 20 Characters.

> 
>  function Create(Str: String) return Message;
> 
>  private
>   type Message is tagged
>    record
>     Text: String_Txt;
>    end record;
> end Messages;
> ------------------------------
> package body Messages is
>  function Create(Str: String) return Message is
>   M: Message;
>  begin
>   M.Text := Str;

If Str'Length /= 20, Constraint_Error will be raised.

>   return M;
>  end Create;
> end Messages;

-- 
Jeff Carter
"Hello! Smelly English K...niggets."
Monty Python & the Holy Grail
08




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Constraint Error
  2004-09-29 23:24 Constraint Error Rick Santa-Cruz
  2004-09-30  0:42 ` Jeffrey Carter
@ 2004-09-30  0:42 ` Stephen Leake
  2004-09-30  1:11 ` tmoran
  2004-09-30 13:47 ` John A. S. Rowlands
  3 siblings, 0 replies; 10+ messages in thread
From: Stephen Leake @ 2004-09-30  0:42 UTC (permalink / raw)
  To: comp.lang.ada

"Rick Santa-Cruz" <rick_santa_cruz75@msn.com> writes:

> Hi,
> 
> how can I avoid the Constraint Error, when executing the following code:
> 
> with Messages;
> 
> procedure Test_Messages is
>  M1: Messages.Message;
>  M2: Messages.Message;
> 
> begin
>  m1:=Messages.Create("This is a test");
>  m2:=Messages.Create("This is a second test");
> end Test_Messages;
> ----------------------------
>  package Messages is
>  type Message is tagged private;
>  subtype String_Txt is String(1..20);

This says that all strings will have exactly 20 characters. Not very
flexible.

See Ada.Strings.Bounded and Ada.Strings.Unbounded, in your favorite
Ada manual or textbook.

-- 
-- Stephe




^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Constraint Error
  2004-09-29 23:24 Constraint Error Rick Santa-Cruz
  2004-09-30  0:42 ` Jeffrey Carter
  2004-09-30  0:42 ` Stephen Leake
@ 2004-09-30  1:11 ` tmoran
  2004-09-30 13:47 ` John A. S. Rowlands
  3 siblings, 0 replies; 10+ messages in thread
From: tmoran @ 2004-09-30  1:11 UTC (permalink / raw)


> m2:=Messages.Create("This is a second test");
will raise Constraint_Error because you attempt to store its 21 characters
into M.Text, which is a 20 character string.  Ada is preventing a buffer
overflow error.

> m1:=Messages.Create("This is a test");
raises Constraint_Error because you attempt to store a 14 character
string into a 20 character M.Text  If you had said, for instance,
  M.Text(1 .. Str'length) := Str;
then it would have stored into the first 14 characters, leaving junk
in the next 6 and no way for a later user of M.Text to know which,
if any, of its characters are non-junk.
Or
  M.Text := Str & (Str'length+1 .. M.Text'length => ' ');
would fill in any unused characters with ' ', which might be what
you would like.

To assign an array (or String, which is just an array of characters)
to another array, their sizes must match.  If they don't match, you
will need to truncate, pad, or just partially fill, as needed.  In
that case you probably want to use a counter or special terminating
character, to remember the "actual" size of the string in M.Text

In the particular case of varying length strings, of course, it would
be easiest to just use Ada.Strings.Bounded or Ada.Strings.Unbounded



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: Constraint Error
  2004-09-29 23:24 Constraint Error Rick Santa-Cruz
                   ` (2 preceding siblings ...)
  2004-09-30  1:11 ` tmoran
@ 2004-09-30 13:47 ` John A. S. Rowlands
  3 siblings, 0 replies; 10+ messages in thread
From: John A. S. Rowlands @ 2004-09-30 13:47 UTC (permalink / raw)


Rick,

Try

   M.Text := Ada.Strings.Fixed.Head(
     Source => Str,
     Count => String_Txt.Size,
     Pad => ' ');

instead of your current assignment to M.Text

John Rowlands
BAE SYSTEMS, Warton, UK

"Rick Santa-Cruz" <rick_santa_cruz75@msn.com> wrote in message
news:<cjfg8e$g6i$00$1@news.t-online.com>...
>
> how can I avoid the Constraint Error, when executing the following code:
> ------------------------------
> package body Messages is
>  function Create(Str: String) return Message is
>   M: Message;
>  begin
>   M.Text := Str;
>   return M;
>  end Create;
> end Messages;





^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2004-09-30 13:47 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-29 23:24 Constraint Error Rick Santa-Cruz
2004-09-30  0:42 ` Jeffrey Carter
2004-09-30  0:42 ` Stephen Leake
2004-09-30  1:11 ` tmoran
2004-09-30 13:47 ` John A. S. Rowlands
  -- strict thread matches above, loose matches on Subject: below --
1999-04-07  0:00 Constraint error Nirmala Bhupathi
1999-04-07  0:00 ` Matthew Heaney
1999-04-07  0:00   ` Nirmala Bhupathi
1999-04-08  0:00     ` Matthew Heaney
1987-04-10 21:47 Constraint_Error keith

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