Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output Integers using the Put_Line method?

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;`
like image 996
W.K.S Avatar asked Dec 21 '11 19:12

W.K.S


3 Answers

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);
like image 91
Shark8 Avatar answered Nov 04 '22 06:11

Shark8


Try this:

Put_Line(Integer'Image(Number) & " x " & Integer'Image(Count) & " = " & Integer'Image(Result));
like image 5
Ondrej Tucny Avatar answered Nov 04 '22 04:11

Ondrej Tucny


You're already have with and use clauses for Ada.Integer_Text_IO, but you're not actually using it.

Change this:

Put_Line(Number & " x " & Count & " = " & Result);

to this:

Put(Number); Put(" x "); Put(Count); Put(" = "); Put(Result); New_Line;

(I normally wouldn't put multiple statements on one line, but in this case it makes sense.)

Note that Integer'Image prepends non-negative integers with a space, something I've always found greatly annoying; Ada.Integer_Text_IO.Put doesn't do that (unless you ask it to).

You could define overloaded "&" functions, something like this:

function "&"(Left: String; Right: Integer) return String is
begin
    return Left & Integer'Image(Right);
end "&";

function "&"(Left: Integer; Right: String) return String is
begin
    return Integer'Image(Left) & Right;
end "&";

which would make your original Put_Line call valid, but the multiple Put calls are probably better style.

like image 5
Keith Thompson Avatar answered Nov 04 '22 06:11

Keith Thompson