17

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;`
Alexander
  • 9,737
  • 4
  • 53
  • 59
W.K.S
  • 9,787
  • 15
  • 75
  • 122

4 Answers4

14

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);
Elias Mårtenson
  • 3,820
  • 23
  • 32
Shark8
  • 4,095
  • 1
  • 17
  • 31
5

Try this:

Put_Line(Integer'Image(Number) & " x " & Integer'Image(Count) & " = " & Integer'Image(Result));
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • 1
    I believe this is the best solution so far unless the author needs to output numbers in a specific format like leading zeroes, decimal precision and the like. Its clear here what types are being used and which procedures are called. – mulander Dec 22 '11 at 21:04
  • One must account for the [leading character](http://www.adaic.org/resources/add_content/standards/05rm/html/RM-3-5.html), mentioned [here](http://stackoverflow.com/a/8596634/230513). – trashgod Dec 23 '11 at 07:05
  • 1
    That leading character may seem like a pain; but think about having to display signs in monospaced columnar-format (for signed-numbers)... it's arguably easier & shorter to grab the first+1..last of the string than prefix a ' ' or '-' conditionally on the sign of the given value. – Shark8 Dec 24 '11 at 01:56
5

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.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Why is multiple `Put` lines preferable? Coming from a c-background `printf`-style is most preferable, followed by concatenation. Then there's the concurrency problem multiple calls to `Put` introduces. Seems excessive to conjure a task for printing over so small an issue when observed behavior appears that `Put` has a similar mechanism already behind it – Assimilater Feb 16 '18 at 22:37
  • Ada doesn't have `printf`, and a `printf`-like solution difficult to make type-seafe. Concatenation doesn't work with integers (unless you overload the `"&"` operator). If you want special formatting (padding, numeric base, etc.), you can apply it in each `Put` call. The name `Put` was deliberately chosen to be short so that multiple calls aren't overly verbose. What concurrency problems does `Put` introduce? – Keith Thompson Feb 17 '18 at 01:24
  • Multiple tasks output to the console, there is no guarantee there won't be a context switch between calls to `Put`. Kind of a classic example of concurrency problems and the need for mutual exclusion tools like mutexes and semaphores used in OS classes... – Assimilater Feb 18 '18 at 02:20
0

Building on the answer (and a comment in another question) from Keith Thompson, here is a full Ada program that can output strings and integers with &, using Put_Line, but without the spaces that Integer'Image would otherwise prepend:

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

procedure Main is

function lstrip(S: String) return String is
begin
    if S(S'First) = ' ' then
        return S(S'First+1 .. S'Last);
    else
        return S;
    end if;
end;

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

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

begin
   Put_Line("x=" & 42);
end Main;
Alexander
  • 9,737
  • 4
  • 53
  • 59