* Wrong Asignment
@ 2014-05-23 23:15 Cedric
2014-05-23 23:28 ` Adam Beneschan
2014-05-23 23:34 ` Cedric
0 siblings, 2 replies; 3+ messages in thread
From: Cedric @ 2014-05-23 23:15 UTC (permalink / raw)
Hi All,
I am learning Ada since a few days. In my book is a simple excercise to write a program. It looks like this:
with Text_IO;
with My_Int_IO;
with My_Flt_IO;
----------------------------------------------------------------------
procedure chapter2_programming_projects_1 is
Celsius : float := 0.0;
Fahrenheit : float := 0.0;
begin
-- Input
Text_IO.Put("Please put in the temperature in Fahrenheit: ");
My_Flt_IO.Get(Item => Fahrenheit);
-- Processing
Celsius := (5/9) x (Fahrenheit - 32);
-- Output
Text_IO.Put(Item => "The temperature in Celsius is ");
My_Flt_IO.Put(Item => Celsius, Fore => 10, After => 2);
end chapter2_programming_projects_1;
The compiler says:
Celsius := (5/9) x (Fahrenheit - 32); -- binary operator expected
Why does this statement result in an binary object?
Kind regards
Cedric
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Wrong Asignment
2014-05-23 23:15 Wrong Asignment Cedric
@ 2014-05-23 23:28 ` Adam Beneschan
2014-05-23 23:34 ` Cedric
1 sibling, 0 replies; 3+ messages in thread
From: Adam Beneschan @ 2014-05-23 23:28 UTC (permalink / raw)
On Friday, May 23, 2014 4:15:26 PM UTC-7, Cedric wrote:
> Hi All,
>
>
>
> I am learning Ada since a few days. In my book is a simple excercise to write a program. It looks like this:
>
> with Text_IO;
> with My_Int_IO;
> with My_Flt_IO;
>
> ----------------------------------------------------------------------
>
> procedure chapter2_programming_projects_1 is
> Celsius : float := 0.0;
> Fahrenheit : float := 0.0;
>
> begin
> -- Input
> Text_IO.Put("Please put in the temperature in Fahrenheit: ");
> My_Flt_IO.Get(Item => Fahrenheit);
>
> -- Processing
>
> Celsius := (5/9) x (Fahrenheit - 32);
The multiplication operator is * (asterisk), not "x".
After you fix this, you will find other errors. Fahrenheit is a float, and 32 is an integer, and Ada doesn't like it when you mix operand types like that. You'll need to change the integers in this expression to floats, like this:
Celsius := (5.0/9.0) * (Fahrenheit - 32.0);
It's actually a good thing that Ada complains about it, because it prevents some misunderstandings. It is very common for new programmers learning C or C++ or Java to try
celsius = (5/9) * (fahrenheit - 32);
and then get on a help forum asking why does it keep outputting 0 for celsius? (It's because 5 and 9 are integers, so the program does integer division which throws away the remainder.)
> Why does this statement result in an binary object?
The error message said "binary operator", not "binary object". A binary operator is an operator like * that takes two operands. It was complaining because it expected a binary operator like *, but you gave it "x" which is an identifier.
-- Adam
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Wrong Asignment
2014-05-23 23:15 Wrong Asignment Cedric
2014-05-23 23:28 ` Adam Beneschan
@ 2014-05-23 23:34 ` Cedric
1 sibling, 0 replies; 3+ messages in thread
From: Cedric @ 2014-05-23 23:34 UTC (permalink / raw)
Hi Adam,
of course your right. Sometimes am blind.
Many thanks for your help.
Regards
Cedric
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-05-23 23:34 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-23 23:15 Wrong Asignment Cedric
2014-05-23 23:28 ` Adam Beneschan
2014-05-23 23:34 ` Cedric
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox