Search code examples
widthada

Ada Integer'Width error


I have this Ada package code which theoretically is well written:

with Ada.Text_IO;
with Ada.Characters.Handling;

package body pkg_procedure is
    procedure Read_Integer(Num : out Integer) is
        Intro : constant Character := ASCII.LF;
        Back : constant Character := ASCII.Del;

        Char : Character;
        Fin : Boolean := False;
        Number : Natural := 0;

        String_Number : String (1 .. Integer'Width – 1);
    begin
        Ada.Text_IO.New_line;
        Ada.Text_IO.Put ("Write down a number and press Enter: ");

        while not Fin loop
            Ada.Text_IO.Get_Immediate (Char);
            if Ada.Characters.Handling.Is_Digit (Char) then
                Number := Number + 1;
                String_Number(Number) := Char;
                Ada.Text_IO.Put (Char);
            elsif Char = Intro then
                Fin := True;
            elsif Number > 0 and Char = Back then
                Ada.Text_IO.Put (ASCII.BS & ' ' & ASCII.BS);
                Number := Number + 1;
            end if;
        end loop;

        Number := Integer'Value (String_Number (1 .. Number));
        Ada.Text_IO.New_line;
        Num := Number;

        exception
            when Constraint_Error =>
                Ada.Text_IO.New_line;
                Ada.Text_IO.Put_Line ("Sorry: " & String_Number & " is too long to store it");
                Num := 0;
    end Read_Integer;
end pkg_procedure;

When I compile the program I obtain an error on this instruction which says: binary operator expected.

I can't fix it. I am totally new to this programming language.


Solution

  • The problem turns out to be that the - in

        String_Number : String (1 .. Integer'Width – 1);
    

    isn’t a plain - but a wide character with encoding e28093 - EN DASH.

    I found this because, having seen that various exploratory changes didn’t show the error, I reverted to your original and tried compiling with -gnatw8 (input is UTF-8) as well as -gnatl for mixing messages with the program text, which resulted in

    13.         String_Number : String (1 .. Integer'Width – 1);
                                                          12
        >>> binary operator expected
        >>> illegal wide character