• 10-06-2005
DrKillPatient
Hey whats up all, I've been learning c++ for a few days and at college today we were given an assignment to create a program which takes an inputted value (centigrade) and outputs a farenheit value. The formula for which is: farenheit = 9/5 x centigrade + 32. My code is as follows:

Code:

```#include<iostream> int main() {   int farenheit;   int centigrade;   farenheit = (9/5)*(centigrade+32);   std::cout << "Enter degrees centigrade :\n";   std::cin >> centigrade;   std::cout << "Farenheit is equal to: " << farenheit << std::endl;   return 0; }```
well, it doesn't work, I was wondering if anyone could offer any insight or help? Forgive my ignorance as I've only been working with c++ for a few days now. Any help would be greated appreciated, peace.
• 10-06-2005
rockytriton
use a double or a float, you don't want to use an integer for this.
• 10-06-2005
DrKillPatient
Thanks for your reply man, it's solved part of the problem, although something still doesn't seem to work properly.

eg: Centigrade 20 = 68 farenheit

but no matter what value I enter for centirade, I get 32 for farenheit?
• 10-06-2005
treenef
try writing the formula properly.

Code:

```F  =  9C  +  32       ---         5```

=> F=((9*C)/5.0)+32

Also don't forget to insert the formula AFTER you have entered the variable 'centigrade'.
• 10-06-2005
Enahs
9/5 *C is the same thing as 9*C /5.
But he is correct, you add the 32 after you do the multiplication.
And you also need to do the math after the cent has been given the proper values from the user.
• 10-06-2005
Boksha
You need to remember that C and C++ execute all commands in the order you put them in. The program you posted would actually do the following:

This line would first calculate 9/5 as integers (becuase you wrote them as integers) the end result will also be an integer, probably 2. (closest thing to 1.75) Next it will add 32 to the centigrade value (which hasn't been set yet so it could be everything), then it will multiply this by 2. Finally, it will assign the result of this calculation to the variable farenheit.
After all that, it will ask the user to fill in a value for centigrade, which is then not used at all before the program ends, and doesn't influence the value of farenheit at all.
Finally, what the other guys said. You'll want to use float variables for this and (not sure about this one, I don't care too much about imperial measurements) the +32 has to be done at the end of the formula.
The correct program would (assuming I haven't made further mistakes) look like this:

Code:

```#include<iostream> int main() {   float farenheit;   float centigrade;   std::cout << "Enter degrees centigrade :\n";   // FIRST get the centigrade value from the user   std::cin >> centigrade;   // THEN calculate the farenheit value.   // Start the calculation off with centigrade so the return value   // will be a float, even though 9, 5 and 32 are integers   farenheit = centigrade*9/5+32;   std::cout << "Farenheit is equal to: " << farenheit << std::endl;   return 0; }```
• 10-06-2005
Jaqui
Quote:

Originally Posted by Enahs
9/5 *C is the same thing as 9*C /5.
But he is correct, you add the 32 after you do the multiplication.
And you also need to do the math after the cent has been given the proper values from the user.

um, 9/5 *C is != 9C/5

9/5 cannot be first the 9C has to be first calculation.

since 9C/5+32 is 9 times the value of Centigrade, divided by 5 plus 32, not 9 divided by 5 times the value of centigrade plus 32. 9/5*C+32
• 10-06-2005
jverkoey
From here: http://www.condoconcepts.com/convers.htm

Tf = (9/5)*Tc+32

Tc = temperature in degrees Celsius
Tf = temperature in degrees Fahrenheit

Therefore, based upon the way this is defined, you need to follow that equation. Anything else will generate invalid results.

Also, you must take in to account that in C++, doing integer division (9/5) will result in the value being chopped to an int (e.g. 9/5 = 1).

Code:

```#include <iostream> using namespace std; int main() {         cout << 9/5 << endl;         cout << 9.0f/5.0f << endl;         cout << (float)9/(float)5 << endl;         return 0; }```
Will output:
Quote:

Originally Posted by output
1
1.8
1.8

So keep that in mind when doing integer division. You must also keep in mind that the variable integer will only hold integer values. As stated above, to store floating point numbers (e.g. 2.422), you would use float, or double. Read up on C++ data types by doing a few Google searches.
• 10-06-2005
VirtualAce
Damn it jverkoey. You keep beating me to posts. Go over to young coders and give them some much needed direction. LOL.

:D (Good to see ya my friend).

Since (9/5) is a constant here - just precompute it.

Code:

`float frac=9.0f/5.0f;`
Then use frac in place of 9/5. Makes it easier to read. You could also #define it.
• 10-06-2005
Enahs
Quote:

Originally Posted by Jaqui
um, 9/5 *C is != 9C/5

9/5 cannot be first the 9C has to be first calculation.

since 9C/5+32 is 9 times the value of Centigrade, divided by 5 plus 32, not 9 divided by 5 times the value of centigrade plus 32. 9/5*C+32

Sorry man, you are wrong about the math.

let's say X = 10

9*X = 90
90/5 = 18

9/5 = 1.8
1.8 * X = 18

And to take 100 degrees C (because we should all know that is 212 F)
[ (9/5) * 100 ] = 180
180 + 32 = 212

9 * 100 = 900
900 / 5 = 180
180 + 32 - 212

You can replace X with any number and it will still get the same exact thing for both methods. That is how multiplication and division work (they are really the same thing). Both ways work (assuming you do them correctly in c++).

• 10-06-2005
Rashakil Fol
Quote:

Originally Posted by Enahs
Sorry man, you are wrong about the math.

let's say X = 10

9*X = 90
90/5 = 18

9/5 = 1.8
1.8 * X = 18

This is where you are incorrect about the math. 9/5 = 1, not 1.8. Get used to the fact that C++ does not use the same style of mathematical notation as what you have been used to.

9 and 5 are integers, and C++'s division function over the integers is not the real-valued division function. That '/' in '9/5' is not the same operator as the '/' in '9.0 / 5.0'.

Quote:

Originally Posted by Bubba
Since (9/5) is a constant here - just precompute it.
Code:
float frac=9.0f/5.0f;

Or... you could just write 1.8 ;-)
• 10-06-2005
Enahs
Quote:

This is where you are incorrect about the math. 9/5 = 1, not 1.8. Get used to the fact that C++ does not use the same style of mathematical notation as what you have been used to.
Which is why I said:
"(assuming you do them correctly in c++)"

We where not discussing c++, but the actual math. Implementing the math in c++ is different though, yes.
• 10-06-2005
Ancient Dragon
Quote:

Originally Posted by Jaqui
um, 9/5 *C is != 9C/5

9/5 cannot be first the 9C has to be first calculation.

since 9C/5+32 is 9 times the value of Centigrade, divided by 5 plus 32, not 9 divided by 5 times the value of centigrade plus 32. 9/5*C+32

Its been a couple years since I was in school, but 9/5*10 is the same as 10*9/5 which is the same as 9*10/5 because of the associative properties of mathametics . So 9/5*C = 9*C/5 = C*9/5.