From: "Alex Mentis" <foo@invalid.invalid>
Subject: Re: index check failure - constraint error
Date: Fri, 1 Apr 2011 11:27:30 +0000 (UTC)
Date: 2011-04-01T11:27:30+00:00 [thread overview]
Message-ID: <in4cr1$uqo$1@dont-email.me> (raw)
In-Reply-To: 738ba46b-dfd1-4a28-9392-0be197a1b6d0@l5g2000vbx.googlegroups.com
tonyg wrote:
> On Apr 1, 11:53�am, tonyg <tonytheg...@gmail.com> wrote:
> > I'm getting a constraint error and I cannot see why...
> >
> > � �function String_To_Integer (The_String : String) return Integer
> > is � � � String_Length : Integer := The_String'length;
> > � � � Return_Value : Integer := 0;
> > � � � subtype Number_Character �is character range '0'..'9';
> > � �begin
> > � � � � Int_Io.Put(String_Length);
> > � � � Ada.Text_IO.Put_Line (The_String);
> > � � � if String_Length > 0 then
> > � � � for count in 1..String_Length loop
> > � � � � �case The_String(count) is
> > � � � � � � when Number_Character =>
> > � � � � � � � �Return_Value := Return_Value +
> > � � � � � � � � �((Character'pos(The_String(count)) - 48) *
> > (10**(count-1)));
> > � � � � � � when others =>
> > � � � � � � � �raise Conversion_Error_Exception;
> > � � � � �end case;
> > � � � � �end loop;
> > � � � else
> > � � � � �Return_Value := 0;
> > � � � end if;
> >
> > � � � return Return_Value;
> >
> > � �end String_To_Integer;
> >
> > I checked to see there was a string going in and it had a length.
> > The actual error was 'index check failure' but as far as I can see
> > everything is present and correct. Can anyone see what it is ?
>
> The index check failure occurred at
>
> case The_String(count) is
>
> btw
Any particular reason you aren't using the Ada-provided Get procedure
that converts a String to an Integer? It operates just like the usual
Get procedure, consuming all Integer-valid characters and stopping when
it gets to a non-digit, but you can make it detect and throw your
constraint with something like this:
with Ada.Text_Io, Ada.Integer_Text_Io;
use Ada.Text_Io;
Procedure Main is
Conversion_Error_Exception : exception;
Input1 : String := "123";
Input2 : String := "1E3";
Input3 : String := "12ab3";
My_Int : Integer;
Length : Positive;
begin
Ada.Integer_Text_Io.Get (Input1, My_Int, Length);
if Length /= Input1'Length then
raise Conversion_Error_Exception;
else
Ada.Integer_Text_Io.Put (My_Int, 0);
New_Line;
end if;
Ada.Integer_Text_Io.Get (Input2, My_Int, Length);
if Length /= Input2'Length then
raise Conversion_Error_Exception;
else
Ada.Integer_Text_Io.Put (My_Int, 0);
New_Line;
end if;
Ada.Integer_Text_Io.Get (Input3, My_Int, Length);
if Length /= Input3'Length then
raise Conversion_Error_Exception;
else
Ada.Integer_Text_Io.Put (My_Int, 0);
New_Line;
end if;
end Main;
next prev parent reply other threads:[~2011-04-01 11:27 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-04-01 10:53 index check failure - constraint error tonyg
2011-04-01 11:07 ` tonyg
2011-04-01 11:27 ` Alex Mentis [this message]
2011-04-01 11:33 ` Alex Mentis
2011-04-01 11:36 ` Alex Mentis
2011-04-01 12:18 ` Georg Bauhaus
2011-04-01 22:20 ` tonyg
2011-04-01 23:37 ` Ludovic Brenta
2011-04-01 11:12 ` Egil Høvik
2011-04-01 11:17 ` tonyg
2011-04-01 12:13 ` Ludovic Brenta
2011-04-01 15:02 ` Adam Beneschan
2011-04-01 17:39 ` Jeffrey Carter
2011-04-01 22:16 ` tonyg
2011-04-01 22:31 ` Jeffrey Carter
2011-04-01 22:49 ` Robert A Duff
2011-04-01 23:00 ` Adam Beneschan
2011-04-01 23:29 ` Robert A Duff
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox