-
wrong result?
Code:
#include<iostream>
#include<conio.h>
using std::cout;
using std::cin;
using std::endl;
// functions prototypes
double Celsius( double );
double Fahrenheit( double );
int main()
{
// declare varibales
double tempCelsius, tempFahrenheit;
// read temperatures
cout<< "Enter temperature in Celsius scale:";
cin>> tempCelsius;
cout<< "\nEnter temperature in Fahrenheit scale:";
cin>> tempFahrenheit;
// display results
cout<< "\n" << tempCelsius << "c = " << Celsius( tempFahrenheit ) << "F";
cout<< "\n" << tempFahrenheit << "F = " << Fahrenheit( tempCelsius ) << "c";
getch();
}
// definitions of Celsius() & Fahrenheit()
double Celsius( double valueA )
{
return (5/9)*(valueA-32);
}
double Fahrenheit( double valueB )
{
return (9/5)*valueB+32;
}
This code gives the right answer for celsius temperature when converted from fahrenheit scale but centigrade scale to fahrenheit it gives only 0F as answer.
help needed/
-
>> (5/9)
Integer division.
gg
-
5/9 = 0
5.0/9.0 = .5555555555555...
-
just a slight change makes it work.
only adding double or static_cast<double> doesn't give the required results on my dev c++ compiler.
y? is that?
Code:
double Celsius( double valueA )
{
return (( double )(5.0/9.0)) *(valueA-32);
}
double Fahrenheit( double valueB )
{
return (( double )(9.0/5.0)) *valueB+32;
}
-
Perhaps you should choose better names
double Celsius( double valueA )
Now is that to Celsius or from Celsius?
And valueA doesn't help. If it were either c or f for example, that might be more of a clue.
Code:
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
// functions prototypes
double Fahrenheit_to_Celsius( double f );
double Celsius_to_Fahrenheit( double c );
int main()
{
for ( double centigrade = 0.0 ; centigrade <= 100.0 ; centigrade += 10.0 ) {
double c_to_f = Celsius_to_Fahrenheit( centigrade ); // convert it
double f_to_c = Fahrenheit_to_Celsius( c_to_f ); // and back again
cout << centigrade << " " << c_to_f << " " << f_to_c << endl;
}
}
// definitions of Celsius() & Fahrenheit()
double Fahrenheit_to_Celsius( double f )
{
double result = f - 32;
result = result * 5.0;
result = result / 9.0;
return result;
}
double Celsius_to_Fahrenheit( double c )
{
double result = c * 9.0;
result = result / 5.0;
result = result + 32;
return result;
}
-