How to output Integers using the Put_Line method?

W.K.S picture W.K.S · Dec 21, 2011 · Viewed 35.5k times · Source

I can't get this program to compile because it doesn't seem to print integer variables along with strings in the Put_Line method. I've looked at source code online and it works when they do it so where am I going wrong. Thanks for your help.

with Ada.Text_IO;                       use Ada.Text_IO;
with Ada.Integer_Text_IO;           use Ada.Integer_Text_IO;

procedure MultiplicationTable is

    procedure Print_Multiplication_Table(Number :in Integer; Multiple :in Integer) is
        Result : Integer;   
    begin
        for Count in 1 ..Multiple
        loop
            Result := Number * Count;
            Put_Line(Number & " x " & Count & " = " & Result);
        end loop; 
    end Print_Multiplication_Table;
    Number  :   Integer;
    Multiple    :   Integer;

begin
    Put("Display the multiplication of number: ");
    Get(Number);
    Put("Display Multiplication until number: ");
    Get(Multiple);
    Print_Multiplication_Table(Number,Multiple);
end MultiplicationTable;`

Answer

Shark8 picture Shark8 · Dec 21, 2011

The problem is that you're using & with strings and integers. Try one of the following:

Replace Number inside the parameter of put with Integer'Image(Number)

Or break up the Put_Line into the components that you want; ex:

-- Correction to Put_Line(Number & " x " & Count & " = " & Result);
Put( Number );
Put( " x " );
Put( Count );
Put( " = " );
Put( Result);
New_Line(1);