From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,1b6f0735bc6de58e X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: "Alex Mentis" Newsgroups: comp.lang.ada Subject: Re: index check failure - constraint error Date: Fri, 1 Apr 2011 11:27:30 +0000 (UTC) Organization: A noiseless patient Spider Message-ID: References: <738ba46b-dfd1-4a28-9392-0be197a1b6d0@l5g2000vbx.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Injection-Date: Fri, 1 Apr 2011 11:27:30 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="TfaOIE1E70h9psK9x8LxRg"; logging-data="31576"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mIIx6Y15riFyi68B95O0LElp/BxkKNSM=" User-Agent: XanaNews/1.19.1.269 Cancel-Lock: sha1:qbmGkfqkf8tEMm+bjrVKN5TSyk0= Xref: g2news2.google.com comp.lang.ada:19633 Date: 2011-04-01T11:27:30+00:00 List-Id: tonyg wrote: > On Apr 1, 11:53�am, tonyg 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;