Temperature conversion

D

deanfamily11

I'm trying to have this program do a simple temperature conversion from
Fahrenheit to Celsius. I have confirmed that the other variable is
receiving and calculating the conversion, but it is just outputting as "0".
Any thoughts? (Code is below)


#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

//declare variable
int fahre;
double centi;

//get the temperature
cout << "Welcome to the temperature converter." << endl << endl;
cout << "Enter a temperature in degrees Fahrenheit: ";
cin >> fahre;
cout << endl;
cout << endl;
cout << endl;

//convert the temperature to centigrade
centi = (fahre - 32) * (5 / 9);

//output both the original temperature and the converted one
cout << "Below is your original and converted temperatures." << endl;
cout << "Degrees F: " << fahre << endl;
cout << "Degrees C: " << centi << endl;

return 0;
}
 
S

Shezan Baig

deanfamily11 said:
I'm trying to have this program do a simple temperature conversion from
Fahrenheit to Celsius. I have confirmed that the other variable is
receiving and calculating the conversion, but it is just outputting as "0".
Any thoughts? (Code is below)

[snip]

//convert the temperature to centigrade
centi = (fahre - 32) * (5 / 9);


Change "(5 / 9)" to "(5.0 / 9.0)". When you do 5/9, it uses integer
arithmetic, so the result becomes 0. So you are essentially doing
"(fahre - 32) * 0"

Hope this helps,
-shez-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top