Log in

View Full Version : No error messages, but output keep


Rebekah_S
Oct 23, 2007, 05:52 PM
I am very new to the world of C++, but was asked to program a Fahrenheit to Celsius converter application. I FINALLY got the code to not give me any error messages, but every time I execute the application, the output answer for celsius is 0. Could anyone help me determine where the problem is??




#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::ios;
using std::setiosflags;


//function prototypes
int getFahrenheit();
int calcCelsius(int, int);


int main()
{
int far = 0;
int subt = 32;
int cel = 0;

far = getFahrenheit();





//display celTemp
cout << setiosflags(ios::fixed) << setprecision(0);
cout << "Celsius temperature is: " << cel << endl;

return 0;
} //end of main function

//*****function definitions*****
int getFahrenheit()
{
double FahrenH = 0.0;
cout << "Enter temperature in Fahrenheit: ";
cin >> FahrenH;
return FahrenH;
} //end of getFahrenheit function

int calcCelsius(int far, int subt)
{
int cel = 0.0;
cel = (5.0*(far - subt)/9.0);
return cel;
} //end of calCelsius function

Marriedguy
Oct 23, 2007, 06:18 PM
I think you are using the wrong data type "int" this for whole numbers. "float" is used for decimal numnbers. Hope this helps.

Rebekah_S
Oct 23, 2007, 06:30 PM
changing to "float" didn't seem to help... my solution keeps being "0" when executed. It seems to be more of a logical-type problem. (i.e. is part of the equation being calculated as 0?)

asterisk_man
Oct 24, 2007, 05:53 AM
You never called the calcCelsius function to set the cel variable in main(). With your current implementation you need to add the following code below where you have far = getFahrenheit():
cel=calcCelsius(far, 32)

On a side note, why is subt a parameter of calcCelsius()? Is there any case where this isn't going to be 32?