1

I'm trying to use Integer'Value to convert a string to an Integer. This works fine for the first loop through a file, but after that I get a bad input for 'value (raised Constraint_Error. I was hoping someone could show me the error of my ways, so that I can convert the string to an integer on each loop.

WITH Ada.Text_IO, Ada.Integer_Text_IO;
USE Ada.Text_IO, Ada.Integer_Text_IO;

PROCEDURE Isbntest IS

  FUNCTION Strip(The_String: String; The_Characters: String)
        RETURN String IS
     Keep: ARRAY (Character) OF Boolean := (OTHERS => True);
     Result: String(The_String'RANGE);
     Last: Natural := Result'First-1;
  BEGIN
     FOR I IN The_Characters'Range LOOP
        Keep(The_Characters(I)) := False;
     END LOOP;
     FOR J IN The_String'RANGE LOOP
        IF Keep(The_String(J)) THEN
           Last := Last+1;
           Result(Last) := The_String(J);
        END IF;
     END LOOP;
     RETURN Result(Result'First .. Last);
  END Strip;


  Input: File_Type := Ada.Text_IO.Standard_Input;

BEGIN

   WHILE NOT End_of_File(Input) LOOP
     DECLARE 
     Line : String := Ada.Text_IO.Get_Line(Input);
     StrippedLine : String := line;
     ascii_val: Integer :=0;

  BEGIN
     StrippedLine := Strip(Line, "-");         
     ascii_val := integer'value(StrippedLine);
     Put(ascii_val);
     Put_line(StrippedLine);
  END;
   END LOOP;
   Close (Input);
end isbntest;
Justiciar
  • 356
  • 1
  • 3
  • 20
  • What do you think will happen if the string you are trying to convert to an integer contains non-numeric characters? – Jim Rogers Oct 02 '17 at 19:29
  • @JimRogers The program's goal is to take ISBN numbers and check them for validity. One of the parameters is that the number can come with any amount of dashes (-) and it's supposed to display the number without them. The function is to remove the dashes. I believe integer'value returns the ascii value of the string, so if it contains non-numeric characters it would throw the integer value off. – Justiciar Oct 02 '17 at 19:47
  • Since ISBNs since 2007 have 13 digits, you probably should use `Long_Integer` (or define your own integer type `range 0 .. 10**13 - 1`) – Simon Wright Oct 03 '17 at 05:10
  • 2
    @SimonWright I don't think Long_Integer is guaranteed to have 13 digits. And in general, please don't encourage people to use the predefined numerical types unnecessarily. – Jacob Sparre Andersen Oct 03 '17 at 05:35
  • Integer'Value returns the integer value of the numeric string. Contraint_Error is raised when the string of digits would result in a value outside the range of Integer. If you only want to see the digits without the dashes, simply print the stripped input string. – Jim Rogers Oct 03 '17 at 13:26

1 Answers1

5

The problem is that you're messing with the length of an array after you created it. Don't do that.

Instead of

  DECLARE 
     Line : String := Ada.Text_IO.Get_Line(Input);
     StrippedLine : String := line;
  BEGIN
     StrippedLine := Strip(Line, "-");   

Just initialise Stripped_Line directly to the correct size when you declare it.

  DECLARE 
     Line : String := Ada.Text_IO.Get_Line(Input);
     StrippedLine : String := Strip(Line, "-"); 
  BEGIN

I'm assuming your "Strip" function works correctly here..