Why is my formula not working ?

May 6, 2014 at 6:16am
My fahrenheit doeesnt work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()

{
	int celsius, Fahrenheit;
	cout << "Enter temperature in Celsius : " ;
	cin >> celsius ;
	Fahrenheit = 9/5*celsius+32 ;

	cout << " Temperature in Celsius " << "\t\t" << " Temperature in Fahrenheit " << endl;
	cout << "\t\t\t\t" << celsius << "\t\t\t\t\t" << Fahrenheit << endl;
			 system("pause");
	    return 0;
}
May 6, 2014 at 6:21am
Hi @danzal
i do not know if
the formula is correct
(google knows) but
i suppose that you are not
getting decimal precision;

Use double types (objects),
variables etc;
or convert temporarily one
of your integers i.e.

1
2
3
int total=4;
int sum=20;
double average=static_cast<double>(sum)/total;



regards!
May 6, 2014 at 6:49am
hi i do not want a decimal answer the answer should be 91 minus the decimal but i get a 65 is there anything wrong ?
May 6, 2014 at 6:56am
Yes, "order"
try this:
 
Fahrenheit = ((9/5)*celsius)+32 ;


EDIT: The order (C++ operator precedence) seems to be ok even without parentheses;
Last edited on May 6, 2014 at 7:07am
May 6, 2014 at 7:05am
Integer division discards remainder. Therefore, 9/5 equals 1 and thus your equation is effectively F=C+32.

If you had written 9.0/5, which has double/int -- which in turn converts to double/double before evaluation -- then you would have floating point division (and both celsius and 32 would convert to doubles before multiplication and addition).

Assignment to integer does discard the fraction too.
May 6, 2014 at 7:12am
I googled the formula and
entering 37 celsius using
your program i got 69

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()

{
        int celsius, Fahrenheit;
        cout << "Enter temperature in Celsius : " ;
        cin >> celsius ;
        Fahrenheit = 9/5*celsius+32 ;

        cout << " Temperature in Celsius " << "\t\t" << " Temperature in Fahrenheit " << endl;
        cout << "\t\t\t\t" << celsius << "\t\t\t\t\t" << Fahrenheit << endl;
                         system("pause");
            return 0;
}



---
Enter temperature in Celsius : 37
 Temperature in Celsius 		 Temperature in Fahrenheit 
				37					69
Last edited on May 6, 2014 at 7:13am
May 6, 2014 at 7:22am
as @keskiverto said
"Integer division discards remainder"
so, try this;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main ()

{
        int celsius, Fahrenheit;
        cout << "Enter temperature in Celsius : " ;
        cin >> celsius ;
        Fahrenheit = 1.8*celsius+32;

        cout << " Temperature in Celsius " << "\t\t" << " Temperature in Fahrenheit " << endl;
        cout << "\t\t\t\t" << celsius << "\t\t\t\t\t" << Fahrenheit << endl;
                         system("pause");
            return 0;
}
May 6, 2014 at 7:23am
Well yeah..37+32 = 69.
The correct answer should be 98.6 or 98 since he wants integer.

Look at keskiverto's post. They show the problem which is integer division the OP wants floating point division.
May 6, 2014 at 11:08am
Well @danzal in resume:
 
Fahrenheit = 9/5*celsius+32;

We found that:

1)Your formula it's right!
2)The "order" or precedence operators
are right!
3)"Integer division discards remainder"
So, 9/5 =1; (instead of 1.8)
Then: (one times C)1*C = C; (celsius)
WrongAnswer=C+32;


EDIT: constant 32;

Last edited on May 6, 2014 at 1:07pm
Topic archived. No new replies allowed.