comp.lang.ada
 help / color / mirror / Atom feed
From: Ted Dennison <dennison@telepath.com>
Subject: Re: integer'image
Date: 2000/04/17
Date: 2000-04-17T00:00:00+00:00	[thread overview]
Message-ID: <8dflqg$8m1$1@nnrp1.deja.com> (raw)
In-Reply-To: 29IK4.5459$sB3.3596@news.indigo.ie

In article <29IK4.5459$sB3.3596@news.indigo.ie>,
  "NANCY HEHIR" <nanceh.ennis@tinet.ie> wrote:
> for x in 1..20 loop
>     integer_string:=integer'image(x);
> end loop;
>
> everything is fine until x=10. This raises a constraint error which I
> interpret to be a problem with mismatched string lenghts. If I

> I'm sure that it is a standard application to convert integers to
> their string images but I can't figure what I'm missing here.

The problem isn't your integer-string conversion idiom, its your string
assignment idiom. You can't expect to be able to assign a varying-length
string into a fixed length string. It'll only work if the lengths of the
two strings just happen to be equal.

There are several ways to save your string image. One is to initialize a
constant (or variable) with the string itself, eg:

   for x in 1..20 loop
      declare
         Integer_String : constant String := Integer'image(x);
      begin
         ...
      end;
   end loop;

Another way would be to declare a string big enough to hold the largest
value you'll get, and keep track of its length:

   for x in 1..20 loop
      Integer_String_Length := String'length(Integer'image(x));
      Integer_String(1..Integer_String_Length) := Integer'image(x);
   end loop;


Another would be to use either Ada.Strings.Unbounded.Unbounded_String or
Ada.Strings.Bounded.Bounded_String.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




  parent reply	other threads:[~2000-04-17  0:00 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-04-17  0:00 integer'image NANCY HEHIR
2000-04-17  0:00 ` integer'image tmoran
2000-04-17  0:00 ` Ted Dennison [this message]
2000-04-18  0:00   ` integer'image Marin D. Condic
2000-04-18  0:00     ` integer'image Ted Dennison
2000-04-18  0:00       ` integer'image Marin D. Condic
2000-04-19  0:00 ` integer'image MaggieJohn
  -- strict thread matches above, loose matches on Subject: below --
1991-01-28  3:27 integer'image Jim West (Stealth Contractor)
1991-01-28 15:04 ` integer'image Michael Feldman
1991-01-28 23:09   ` integer'image (George C. Harrison) Norfolk State University
1991-01-29 19:55     ` integer'image stephen edwards
     [not found] <2563@sparko.gwu.edu>
1991-01-18 19:13 ` integer'image Arthur Evans
1991-01-18 20:32   ` integer'image Michael Feldman
1991-01-20  2:54   ` integer'image       Hoysch
1991-01-20 23:10     ` integer'image Michael Feldman
1991-01-21 15:23     ` integer'image Jerry Callen
1991-01-21 19:41       ` integer'image Steven V. Case
1991-01-21 21:54         ` integer'image Jerry Callen
1991-01-22 19:21         ` integer'image Michael Feldman
1991-01-22 21:37           ` integer'image Richard Pattis
1991-01-23 16:29             ` integer'image Michael Feldman
1991-01-18 23:05 ` integer'image (George C. Harrison) Norfolk State University
1991-01-19 10:51 ` integer'image Jean Pierre Rosen
replies disabled

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