How to output Integers using the Put_Line method?
Asked Answered
S

4

18

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;`
Slesvig answered 21/12, 2011 at 19:49 Comment(0)
L
15

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);
Luxurious answered 21/12, 2011 at 20:10 Comment(2)
Why New_Line(1); rather than just New_Line;?Mantelletta
@Keith -- Honestly: I didn't remember if the default was 1, or even if the default existed, but did remember there was a parameter.Luxurious
I
5

Try this:

Put_Line(Integer'Image(Number) & " x " & Integer'Image(Count) & " = " & Integer'Image(Result));
Inanition answered 21/12, 2011 at 20:9 Comment(3)
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.Pomade
One must account for the leading character, mentioned here.Quilmes
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.Luxurious
M
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.

Mantelletta answered 21/12, 2011 at 21:29 Comment(3)
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 itPorbeagle
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?Mantelletta
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...Porbeagle
N
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;
Nez answered 17/12, 2017 at 20:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.