Vaaljan
Vaaljan

Reputation: 832

Converting Celsius to Fahrenheit and Vice Versa in Delphi

I am Studying Delphi, For some reason they use this old language but any case. I need to build a Weather application that Converts Celsius And Fahrenheit.

The user has to enter 7 days Weather but This example I just want to figure out Day 1

So I have first of all created Radio buttons to Choose Celsius and Fahrenheit, Then if they choose Celsius I have to Convert Celsius to Fahrenheit and if they choose Fahrenheit I have to Convert it to Celsius.

So This is my Code:

 var
   Celsius, Fahrenheit, Day1 : Double;

 begin
   Day1 := fedDay1;
   Celsius := (Day1 - 32)*5/9;
   Fahrenheit := Day1 * 9/5 + 32;

   if radCelsius.checked then
     lblConvertDay1.Caption := FloatToStrF(Fahrenheit, ffFixed, 15, 2);
   else
     lblConvertDay1.Caption := FloatToStr(Celsius, ffFixed, 15, 2);
   end;

It gives an Error on fedDay1 This is a Floating Spin Edit

Help please

Upvotes: 1

Views: 1384

Answers (4)

Ken White
Ken White

Reputation: 125728

You can't assign the SpinEdit to the variable directly. You have to use the value.

Day1 := fedDay1.Value;

Your code has to only do the conversion needed, as Day1 will already have one of them. So if the user entered a Celsius value, you only need to convert from Celsius to Fahrenheit. If they entered a Fahrenheit value, you only need to convert from Fahrenheit to Celsius.

var
  Day1: Double;
  OutVal: Double;
begin
  Day1 := fedDay1.Value;
  if radCelsius.checked then
    OutVal := (9.0 / 5.0) * Day1 + 32     // Cels. entered; convert to Fahrenheit 
  else
    OutVal := (5.0 / 9.0) * (Day1 - 32);  // Fahr. entered; convert to Celsius
  lblConvertDay1.Caption := FloatToStr(OutVal, ffFixed, 15, 2);
end;

If you're using a recent version of Delphi (Delphi 2007 has them, and they may go as far back as Delphi 5), the StdConvs unit has predefined functions you can use for the conversions:

implementation

uses
  StdConvs;

   if ...
      OutVal := CelsiusToFahrenheit(Day1)
   else
      OutVal := FahrenheitToCelsius(Day1);

Upvotes: 5

David Heffernan
David Heffernan

Reputation: 613302

The right way to solve the problem is to create a function to convert from Celsius to Fahrenheit, and another to convert in the opposite direction.

function CelsiusFromFahrenheit(const Fahrenheit: Double): Double;
begin
  Result := (Fahrenheit-32.0)/1.8;
end;

function FahrenheitFromCelsius(const Celsius: Double): Double;
begin
  Result := Celsius*1.8 + 32.0;
end;

The main error in your code is that fedDay1 is not a floating point value, but a GUI control. So you need to read the value out of the control with fedDay1.Value.

Then you can write your GUI update code much more cleanly:

InputValue := fedDay1.Value;
if radCelsius.checked then
  ConvertedValue := FahrenheitFromCelsius(InputValue)
else
  ConvertedValue := CelsiusFromFahrenheit(InputValue);
lblConvertDay1.Caption := FloatToStrF(ConvertedValue, ffFixed, 15, 2);

Upvotes: 3

RBA
RBA

Reputation: 12584

You have the function CelsiusToFahrenheit. Your code needs some modifications in order to have a functional application.

Upvotes: 1

Stanley De Boer
Stanley De Boer

Reputation: 5241

You can't assign the spinner to the double. You need to get the value from it.

Try:

Day1 := fedDay1.Value;

Upvotes: 2

Related Questions