# Why is this program converting to integers

• 05-31-2004
3DPhreak
Why is this program converting to integers
Code:

```// This program converts Celcius to Fahrenheit #include <iostream.h> #include <iomanip.h> void main(void) {         float Celcius, Fahrenheit;         cout << "Please enter a a temperature in Celcius: ";         cin >> Celcius;         Fahrenheit = ((9.0/5.0)*Celcius) + 32;         cout.setf(ios::fixed);         cout.precision(1);         cout << endl;         cout << "The temperature you entered is " << Fahrenheit << " degrees Fahrenheit." << endl << endl; }```
Note the Fahrenheit calculation. If I use the following, it simply doesn't work:
Fahrenheit = ((9/5)*Celcius) + 32;

It's taking the 9/5 and converting it to an integer value of 1. I had to use the decimal to get it to work. Is this a feature of my compiler? - Visual C++ 6.0

Why would it do this?
• 05-31-2004
Thantos
9/5 comes out to one because it sees the 9 and the 5 and goes "ok those are integers" and follows the normal integer division rules.

So by saying 9.0/5.0 it sees the 9.0 and the 5.0 and goes "ok those are doubles" and follows the normal double division rules.
• 05-31-2004
3DPhreak
Quote:

Originally Posted by Thantos
9/5 comes out to one because it sees the 9 and the 5 and goes "ok those are integers" and follows the normal integer division rules.

So by saying 9.0/5.0 it sees the 9.0 and the 5.0 and goes "ok those are doubles" and follows the normal double division rules.

Thanks! I figured that's what it was doing, but I just can't fathom why they would make it that way. Is this normal? I would imagine it would have promoted those to floating points since Celcius is a float as well.

So many things to learn and understand.......

Thanks again for helping me out on this one.....
• 05-31-2004
Thantos
It is normal. Most compilers will actually do the division during compile time and store the result and during run time multiple it by Celcius.