I am supposed to write a program that allows the user to convert a temperature either from celsius to fahrenheit or fahrenheit to celsius and use separate functions for each conversion. I'm not sure if I'm headed in the right direction with my code, no matter what number I put in it always gives back the number 1.
#include <iostream>
usingnamespace std;
int CelsiusToFahrenheit(int nTempc, int nTempf) {
int nResult;
int i;
nResult = 1;
for (i = 1; i <= nTempf; i++) {
nTempf = nTempc * (9 / 5) + 32;
}
return nResult;
}
int FahrenheittoCelsius (int nTempf, int nTempc) {
int nResult;
int i;
for (i = 1; i <= nTempc; i++) {
nTempc = (nTempf - 32) * (5 / 9);
}
return nResult;
}
int main() {
int nInteger;
int nTempc;
int nTempf;
int nResult;
int i;
cout << "Convert C to F (enter 1) or F to C (enter 2): ";
cin >> nInteger;
while (nInteger < 1 || nInteger > 2) {
cout << "Invalid entry\n";
cout << "Convert C to F (enter 1) or F to C (enter 2): ";
cin >> nInteger;
}
nResult = CelsiusToFahrenheit(nTempc, nTempf);
if (nInteger == 1) {
cout << "Please enter a temperature in Celsius: ";
cin >> nTempc;
}
if (nInteger == 1) {
cout << nTempc << " Celsius equals " << nResult << " Fahrenheit";
}
nResult = FahrenheittoCelsius(nTempf, nTempc);
if (nInteger == 2) {
cout << "Please enter a temperature in Fahrenheit: ";
cin >> nTempf;
}
if (nInteger == 2) {
cout << nTempf << " Fahrenheit equals " << nResult << " Celsius";
}
return 0;
}
Please update edit your post and put your code between code tags to make it more readable. It's under the format section as <> - http://www.cplusplus.com/articles/jEywvCM9/