From: Adam Beneschan <adam@irvine.com>
Subject: Re: Ada Recursion with strings
Date: Tue, 30 Sep 2008 15:24:40 -0700 (PDT)
Date: 2008-09-30T15:24:40-07:00 [thread overview]
Message-ID: <e71f84d2-c65a-436d-84aa-322219e68531@e17g2000hsg.googlegroups.com> (raw)
In-Reply-To: ff8b4b01-211b-4487-8184-85563f4148db@t42g2000hsg.googlegroups.com
On Sep 30, 1:30 pm, Joe <joesmo...@gmail.com> wrote:
> Hey all,
> I'm trying to write a function that converts a 2**32 mod type into an
> string formatted like an IP address (i.e. 192.168.2.1).
>
> Here's what I wrote
>
> with Ada.Text_IO;
> use Ada.Text_IO;
>
> procedure Ip_packet is
> type Word_Type is mod 2**32;
>
> function IP_String (Word : Word_Type;
> IP_String : String := "") return String
> is
> Sub_String : String := Word_Type'Image(Word mod 2**8);
> begin
> if Word <= 2**8 then
> return Word_Type'Image(Word) & IP_String;
> else
> return IP_String( Word / 2**8,
> "." & Sub_String & IP_String);
> end if;
> end IP_String;
>
> begin -- Ip_packet
> Put(IP_String(123456));
> end Ip_packet;
>
> There are 2 errors with this that I don't understand. First, Ada
> expects an integer for (word / 2**8). Is this because word_type is
> outside the standard integer range? How do I get a division operator
> that works on word_type?
>
> If I patch the integer error by type casting word to an integer I get
> another error. On the second return statement, Ada complains that
> there are "too many subscripts in array reference". Is there a
> limitation on the string I can pass to a recursive function call?
When you say this:
return IP_String( Word / 2**8,
"." & Sub_String & IP_String);
what is the compiler going to think you mean by the first IP_String in
this expression? What about the second one?
There are other errors in your code; you'll probably figure several of
them out once you get your program to run, and you'll probably figure
another out if you change 123456 in your test to 65536.
-- Adam
next prev parent reply other threads:[~2008-09-30 22:24 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-30 20:30 Ada Recursion with strings Joe
2008-09-30 22:24 ` Adam Beneschan [this message]
2008-09-30 22:24 ` Jeffrey R. Carter
2008-10-01 0:13 ` Joe
2008-10-01 0:27 ` Jeffrey R. Carter
2008-10-01 1:15 ` Robert A Duff
2008-10-01 11:34 ` Jeffrey R. Carter
2008-10-01 14:29 ` Adam Beneschan
2008-10-01 15:18 ` Jean-Pierre Rosen
2008-10-01 16:47 ` Georg Bauhaus
2008-10-02 7:24 ` Niklas Holsti
2008-10-02 8:18 ` Ludovic Brenta
2008-10-03 6:54 ` Maciej Sobczak
2008-10-03 7:38 ` Jean-Pierre Rosen
2008-10-03 9:06 ` Dmitry A. Kazakov
2008-10-01 0:28 ` Adam Beneschan
2008-10-01 9:48 ` Georg Bauhaus
2008-10-01 10:22 ` Dmitry A. Kazakov
2008-10-01 3:01 ` anon
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox