comp.lang.ada
 help / color / mirror / Atom feed
* How do i print stuff?
@ 2017-10-28 18:07 tclwarrior
  2017-10-28 18:30 ` Niklas Holsti
  0 siblings, 1 reply; 3+ messages in thread
From: tclwarrior @ 2017-10-28 18:07 UTC (permalink / raw)


Hi,

This is my first day learning Ada 
I know other languages (Mainly Perl and SQL) 
So I am not total newbie

Anyway, 

After that I saw this tweet https://twitter.com/shishini/status/924328446490218496 
I decided that my first program, would be exactly that 

I came up with this 

-- Code
with Ada.Text_IO;


procedure Main is
   a : Float := (0.3 - 0.2);
   b : Float := (0.2 - 0.1);
   c : Boolean := (a=b);
begin
   if c then
      Ada.Text_IO.Put_Line("true");
   else 
      Ada.Text_IO.Put_Line("false");
   End if;
      
end Main;
-- Code 

Which at least show me that Ada is more like Go and D 

But, I failed miserable to simply Print a, b and c 
well, I kind of succeeded in printing a and b using 
Float_Text_IO.Put 

But could not find a way to Print c (a boolean)

Well, so my question, that is the strategy to print stuff in Ada 
does have have a print statement that can print any type 
does each type have his one print statement 
Should I cast all types to strings and print them using Text_IO
How do I cast floats and booleans to strings?

Thanks
Ali


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How do i print stuff?
  2017-10-28 18:07 How do i print stuff? tclwarrior
@ 2017-10-28 18:30 ` Niklas Holsti
  2017-10-28 18:52   ` Niklas Holsti
  0 siblings, 1 reply; 3+ messages in thread
From: Niklas Holsti @ 2017-10-28 18:30 UTC (permalink / raw)


On 17-10-28 21:07 , tclwarrior@gmail.com wrote:
> Hi,
>
> This is my first day learning Ada

Heartily welcome!

   ...
>    if c then
>       Ada.Text_IO.Put_Line("true");
>    else
>       Ada.Text_IO.Put_Line("false");
>    End if;
    ...
> But, I failed miserable to simply Print a, b and c
> well, I kind of succeeded in printing a and b using
> Float_Text_IO.Put

What do you mean "kind of" ? Text_IO is the "print" in Ada.

> But could not find a way to Print c (a boolean)

Boolean is an enumeration type, so the canonical way would be to create 
an instance of the package Ada.Text_IO.Enumeration_IO for the Boolean 
type, and use that. The instance will provide both input and output.

If all you need is to print a Boolean value, you can use the attribute 
function Boolean'Image to convert a Boolean value to a string. Here

    Boolean'Image (True) = "TRUE"
    Boolean'Image (False) = "FALSE"

so you can do

    Ada.Text_IO.Put_Line (Boolean'Image (c));

and get TRUE or FALSE printed.

> Well, so my question, that is the strategy to print stuff in Ada
> does have have a print statement that can print any type

No, there is no such print statement.

> does each type have his one print statement

In principle, yes; there are a set of generic packages that you can 
instantiate to provide I/O for your types.

> How do I cast floats and booleans to strings?

Most scalar types have a 'Image attribute.

The inverse attribute is 'Value, which converts a string into a value. 
For example, Boolean'Value ("TRUE") = True.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How do i print stuff?
  2017-10-28 18:30 ` Niklas Holsti
@ 2017-10-28 18:52   ` Niklas Holsti
  0 siblings, 0 replies; 3+ messages in thread
From: Niklas Holsti @ 2017-10-28 18:52 UTC (permalink / raw)


On 17-10-28 21:30 , Niklas Holsti wrote:
> On 17-10-28 21:07 , tclwarrior@gmail.com wrote:
    ...
>> But could not find a way to Print c (a boolean)
>
> so you can do
>
>    Ada.Text_IO.Put_Line (Boolean'Image (c));
>
> and get TRUE or FALSE printed.

Oh yes, in compilers that implement the most recent language standard, 
you can also use 'Image directly on the variable, as in

    Ada.Text_IO.Put_Line (c'Image);

If you are using the GNAT compiler, and the above does not compile, you 
can use instead the older GNAT-specific form:

    Ada.Text_IO.Put_Line (c'Img);

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-10-28 18:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-28 18:07 How do i print stuff? tclwarrior
2017-10-28 18:30 ` Niklas Holsti
2017-10-28 18:52   ` Niklas Holsti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox