Hey guys, I am starting a c++ module in college and we were asked to write a temperature convertion program to convert from celcius to faranheit. The program works in the code i have below. But the strange thing is when i put brackets around 5/9 or 9/5 in the formulae, the result is different and very wrong. My lecturer is stumped as well as myself because the code compiles perfectly and mathematically there should be no change to the formulae at all.
Any suggestions as to what is wrong here?
// this program is designed to convert temperatures between degrees and celcius
#include <iostream>
using namespace std;
int main()
{
float celcius, fahreinheit;
int option;
cout << "\nIf you would like to convert from celcius to fahreinheit, please press 1.\nOtherwise if you would like to convert from fahreinheit to celcius,\nplease press 2\n";
cin >> option;
if (option == 1)
{
cout << "Please enter temperature to convert in celcius.\n";
cin >> celcius;
fahreinheit = celcius * 9 / 5 + 32;
cout << celcius << " degrees celcius is equal to:\n" << fahreinheit << " degrees fahreinheit\n";
}
else if (option == 2)
{
cout << "Please enter temperature to convert in fahreinheit.\n";
cin >> fahreinheit;
celcius = (fahreinheit - 32)*5/9;
cout << fahreinheit << " degrees fahreinheit is equal to:\n" << celcius << " degrees celcius\n";
}
else
cout << "This is not a valid response";
return 0;
}
Any suggestions as to what is wrong here?
// this program is designed to convert temperatures between degrees and celcius
#include <iostream>
using namespace std;
int main()
{
float celcius, fahreinheit;
int option;
cout << "\nIf you would like to convert from celcius to fahreinheit, please press 1.\nOtherwise if you would like to convert from fahreinheit to celcius,\nplease press 2\n";
cin >> option;
if (option == 1)
{
cout << "Please enter temperature to convert in celcius.\n";
cin >> celcius;
fahreinheit = celcius * 9 / 5 + 32;
cout << celcius << " degrees celcius is equal to:\n" << fahreinheit << " degrees fahreinheit\n";
}
else if (option == 2)
{
cout << "Please enter temperature to convert in fahreinheit.\n";
cin >> fahreinheit;
celcius = (fahreinheit - 32)*5/9;
cout << fahreinheit << " degrees fahreinheit is equal to:\n" << celcius << " degrees celcius\n";
}
else
cout << "This is not a valid response";
return 0;
}