Temperature calculation is getting weird result

Hey guys, I want to know what I did wrong for my Fahrenheit to Celsius function to calculate the wrong answer. Example: If I input 0 degrees for Fahrenheit it results in -0 degrees Celsius. What's causing this? Celsius to Fahrenheit on the other hand seems to work perfectly. I don't know what gives... :-(
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  #include <iostream>
using namespace std;

void f2c ()
{
double degreeF, DEGc;
cout << "Please enter a temperature in Fahrenheit. " << endl;
cin >> degreeF;
cout << "You entered: " << degreeF << " degrees Fahrenheit." << endl;
DEGc = ((degreeF-32)*(5/9));
cout << degreeF << " Fahrenheit is : " << DEGc << " Celcius" << endl;
}

void c2f ()
{
double degreeC, DEGf;
cout << "Please enter a temperature in Celcius. " << endl;
cin >> degreeC;
cout << "You entered: " << degreeC << " degrees Celsius." << endl;
DEGf = (degreeC * (9/5)) + 32;
cout << degreeC << " Celsius is : " << DEGf << " Fahrenheit" << endl;
}

int main ()
{

char unit;

cout << "Press f if you want to enter a value you Fahrenheit or Press c if you want Celsius" << endl;
cin >> unit;

switch (unit)
{
case 'f':
cout << "You chose fahrenheit" << endl;
f2c ();
break;
case 'c':
cout << "You chose celsius" << endl;
c2f ();
break;
default:
cout << "Error you entered an invalid character." << endl;
break;
}

system("pause");
return 0;
}
Last edited on
closed account (21vXjE8b)
There is something in the syntax of the equation...
I put DEGc = (((degreeF - 32) * 5) / 9); and DEGf = (degreeC * 9)/5 + 32; and got the expected result.
If someone could explain this... =D
***The celsius to fahrenheit isn't correct, it gives the wrong answer too.

***EDIT***
Yeah, it's the syntax.
Look at this: DEGc = ((degreeF-32)*(5/9));
In this format, the code'll divide 5 with 9 and subtract 32 of degreeF at the same time... BUT you are dividing to integers numbers (5 and 9), so the result printed by the program is an INTEGER VALUE. Dividing 5 with 9 you have 0.5555, but the code'll print only 0. When degreeF-32 multiplies 0 the result is 0 always.
The same logic happens with celsius to farenheit.
Last edited on
Line 10 DEGc = ((degreeF-32)*(5/9));

change to: DEGc = ((degreeF-32)*5/9);

You want the formula to do subtraction before division of 5/9.
Hi,
Firstly :
DEGc = ((degreeF-32)*(5/9));

==>
DEGc = ((degreeF-32.0)*(5.0/9.0));
Because if they are integers, there are quotient & remainder. But if they are double (eg: 32.0), none of these things matters anymore.
Also :
DEGf = (degreeC * (9/5)) + 32;

==>
DEGf = (degreeC * (9.0/5.0)) + 32.0;
Does that help you? :)
Thanks for your thoughts and input everyone :)

closed account,

Yes that definitely helped and fixed the issue. So generally, when using double and using mathematical calculations, one should use a decimal point to differentiate it from an integer value? :)
> So generally, when using double and using mathematical calculations, one should use a decimal point to differentiate it from an integer value? :)
Yes :)
Topic archived. No new replies allowed.