ljjandre ljjandre - 1 month ago 7
C++ Question

Returning values from a separate function in C++?

I am doing a programming assignment in my CS class using C++ that is giving me problems. The assignment is to write a program that asks the user to input a Fahrenheit temperature. The program must then output the Celsius equivalent of that temperature and the Celsius equivalents of the next 20 Fahrenheit temperatures. This must all be done using a separate function to complete the conversion calculation as well as a function prototype.

I got everything to work except my Celsius outputs all show a value of 0. I cannot seem to find any errors or figure out what is wrong...Please Help!

#include <iostream>
#include <iomanip>

using namespace std;

double Celsius(double temp);

int main() {

double fTemp, cTemp;

cout << "Please input a temperature in degrees Fahrenheit: ";
cin >> fTemp;

cout << "F Temp" << " " << "C Temp"<< endl;
cout << "-------------------" << endl;

for(int count=1; count<= 20; count++){

cTemp = Celsius(fTemp);

cout << setw(6) << fTemp << setw(12) << cTemp<< endl;

fTemp++;

}


return 0;
}

//*************Celsius Function****************//

double Celsius(double temp){

double newC;
newC = (temp-32) * (5/9);
return newC;

}
//************************************************

Answer

(5/9) is integer-integer division. You might've as well typed 0 there. Replace it with (5.0/9.0) and it should be fine.

Comments