Alright, my homework assignment is by using separate function definitions convert a temperature from Fahrenheit to Celcius.

I've looked over the whole chapter, looked at the examples, and am having trouble somewhere. My program will only convert one temperature correctly to Celcius, which is 32, because it always returns a value of 0.

Can someone look over it and see where I went wrong? As far as I can tell it should work, but it doesn't.

``````//Ch9AppE01.cpp
//Converts a Fahrenheit temperature
//to a Celsius temperature
//Created/revised by Greg Schader on 7/5/09

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
int getFahrenheit();
int calcCelcius(int);

int main()
{
//Define Variables.
int degFahrenheit = 0;
int degCelcius = 0;

degFahrenheit = getFahrenheit();

degCelcius = calcCelcius(degFahrenheit);

cout << fixed << setprecision(0) << "Temperature in Celcius: " << degCelcius << endl;

return 0;
}	//end of main function

//*****function definitions*****
int getFahrenheit()
{
int degFahrenheit = 0;
cout << "Enter the temperature in Fahrenheit: ";
cin >> degFahrenheit;
return degFahrenheit;
}

int calcCelcius(int degFahrenheit)
{
int degCelcius = 0;
degCelcius = 5 / 9 * (degFahrenheit - 32);
return degCelcius;
}``````
4
Contributors
8
Replies
9
Views
9 Years
Discussion Span
Last Post by Salem

I think your formula is incorrect. See this

``````int main()
{
float f = 82;
float c = (f - 32.0F) / 1.8F;
cout << "c = " << c << "\n";
}``````

You should add another parentheses `(5/9)*(°-32)` .
Why are you 'fixing' and using setprecisioin() if you're using integers?
Why not use float or double instead of int?

When I use your formula, the program works as intended, but in the textbook the supplied formula is:

Algorithm: Celcius Temperature : 5.0 / 9.0 * (Fahrenheit Temperature - 32.0).

And the celcius value must be returned as an integer.

You should add another parentheses (5/9)*(°-32) .
Why are you 'fixing' and using setprecisioin() if you're using integers?
Why not use float or double instead of int?

The fixed and setprecision was already coded in when I opened the sln file.

I added in the other parentheses and the value is still returned as 0.

The book is wrong because if you use 5/9*(x) = (x*9)/5
Try the simpler constant version AD posted.

5/9 is done as INTEGER arithmetic before it ever gets chance to be promoted to a double.
You always end up with zero in this case.

Make them doubles to begin with.

``````//Ch9AppE01.cpp
//Converts a Fahrenheit temperature
//to a Celsius temperature
//Created/revised by Greg Schader on 7/5/09

#include <iostream>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::fixed;

//function prototypes
int getFahrenheit();
int calcCelcius(int);

int main()
{
//Define Variables.
int degFahrenheit = 0;
int degCelcius = 0;

degFahrenheit = getFahrenheit();

degCelcius = calcCelcius(degFahrenheit);

cout << fixed << setprecision(0) << "Temperature in Celcius: " << degCelcius << endl;

return 0;
}	//end of main function

//*****function definitions*****
int getFahrenheit()
{
int degFahrenheit = 0;
cout << "Enter the temperature in Fahrenheit: ";
cin >> degFahrenheit;
return degFahrenheit;
}

int calcCelcius(int degFahrenheit)
{
int degCelcius = 0;
degCelcius = static_cast<int>((degFahrenheit - 32) / 1.8);
return degCelcius;
}``````

This program works. We'll see what my professor says when she sees I used a different formula. = /

Alright, that fixes my problem. Thanks for the help! Sorry it was such a silly problem lol.

5/9 is done as INTEGER arithmetic before it ever gets chance to be promoted to a double.
You always end up with zero in this case.

Make them doubles to begin with.

So you're saying to just leave them as doubles, but use my fixed and setprecision to convert them back later?

Well if you do
( 5 * (degFahrenheit-32) ) / 9

It's just a matter of rearranging the expression into something which produces a meaningful computational result.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.