The problem is:
"The formula for converting temp. from Fahrenheit to Celsius is c = 5/9 * (F-32) where F is the Fahrenheit temp. and C is the Celsius temp. Write a function named Celsius that accepts a Fahrenheit temp as an argument and returns the temp converted to Celsius. Demonstrate the function by calling it in a loop that displays a table of the Fahrenheit temps 0 through 20 and their Celsius equivalents.
The output of this code gives me the proper Fahrenheit temps (0 through 20) but the Celsius is all coming up as zeros. I dont know if I'm returning the value of Celsius into the loop wrong or what.
Looks like 5/9 is getting evaluated to 0 (hence the output). This is because 5 and 9 are being assumed to be integers. Integer division gives you a remainder and a quotient.
5/9 gives the quotient which is 0.
5%9 would give you the remainder which if you notice is 5.
What you need to do is force the compiler to take decimal quotient. So simply type caste 9 to float like so: 5/(float)9 (note that you can also typecast 5 to float instead of 9)
That's explicit, 9 is forced to be taken as float and 5 is implicitly considered as a float as well (you could explicitly typecast both as float no harm and no difference).
You could also instead of explicitly typecasting, write 5/9.0 instead. Then the compiler takes 9 as float (and hence 5 as well). But I like explicitly mentioning the types of the operands for clarity.
You need to be careful while working with divisions ;)
I agree with most of what Nwb says except for one part:
You could also instead of explicitly typecasting, write 5/9.0 instead. Then the compiler takes 9 as float (and hence 5 as well). But I like explicitly mentioning the types of the operands for clarity.
These days the newer IDEs an compilers interpret "9.0" as a d"double" because "double" is preferred over a "float".
You will see in the function the simple way to fix the problem. This concept will work any time you are doing division and need the decimal point.
#include<iostream>
#include<iomanip>
usingnamespace std; // <---Best not to use.
double get_celsius(int fTemp);
int main()
{
int fTemp = 0;
cout << " Fahrenheit to Celsius \n" << "Fahrenheit Celsius \n" << "-------------- \n";
std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Added.
for (fTemp; fTemp <= 20; fTemp++)
{
cout << std::setw(2) << fTemp << std::setw(8) << ' ' << std::setw(8) << get_celsius(fTemp) << endl; // <--- Changed.
}
// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
// <--- Used to keep the console window open in Visual Studio Debug mode.
// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
double get_celsius(int fTemp)
{
double cTemp;
cTemp = (fTemp - 32) * (5.0 / 9.0);
return cTemp;
}
In the output I did not work with the headings or the dashes. You can work on that part.
In the program notice the comments of what I added or changed. The part in bold is the simple fix to your problem. You could type cast the numbers, but I would suggest using "static_cast<double>(5)" and you could use the same for the "9" although the "9" would be promoted to a "double" before the division is done.
Line 17 changes the output to line everything up in a nicer manner.
After working with the program I came up with some changes:
1 2 3 4 5 6 7 8 9 10 11 12 13
constexprdouble MAXTEMPS{ 20 }; // <--- Added.
double fTemp{}; // <--- Changed.
cout << " Fahrenheit to Celsius Converter\n\n" << "Fahrenheit Celsius \n"; // <--- Changed. Added 3 spaces between "Fahrenheit" and "Celsius".
std::cout << std::setfill('-') << std::setw(23) << '-' << std::setfill(' ') << std::endl; // <--- Changing the 23 will change the length of the dashes. Changed from the original 20.
std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Added. Needs done only once.
for (fTemp; fTemp <= MAXTEMPS; fTemp++) // <--- Changed.
{
cout << std::setw(5) << fTemp << std::setw(10) << ' ' << std::setw(8) << get_celsius(fTemp) << endl; // <--- Changed.
}
Since most of the program works with doubles I changed "fTemp" to a double. This also gives the opportunity to use more than a whole number for "fTemp. I adjusted the output to allow for the decimal number.
BTW from C++11 on you can initialize a variable as "double fTemp{};". The empty {}s will initialize the variable to 0.0 in this case. Yo can also put a number inside the {}s if needed.
It is a floating point calculation using type double. By default C/C++ floating point constants are type double. If you want your constant variables to be a type float you must inform the compiler by using the 'f' suffix, ie 5.0f / 9.0f. Notice that both sides of the calculation require a type float, or else you get type double. By the way most of the C math functions use type double so, except for specialized circumstances, you should be using doubles instead of floats.
I was under assumption that float was the default for decimal values
Why are you always guessing instead of doing some actual research? In case you failed to read the above paragraph, your assumption is wrong again.
And you're saying that in 5/9.0 both are taken as double?
What you have here is an implicit conversion of the integer constant 5 to a double before the floating point calculation.
Did you not read the last paragraph? In that calculation the 5 will be implicitly promoted to a double before the calculation because the constant 9.0 is a type double. Both sides of any calculation must have identical types.
You need to find and read some documentation for "integer and floating point promotion rules in c".
That's fine, but what difference does it make..............................................
Have you tried to find some documentation to read?
By the way this:
So why is 5.0/9.0 better than 5/9.0 if they're both floating point calculation using type double?
Is not the same question I was answering. The question was something about why "5/9.0" wasn't a floating point calculation before you changed the post.
To answer your latest question:
So 5 is being "promoted" in 5/9.0 and is taken as double itself in 5.0/9.0. That's fine, but what difference does it make..............................................
By using floating point constants on both sides of the calculation it eliminates the hidden implicit cast avoiding any ambiguity.
Hey @nwb , @jib , and @HandyAndy , Thank you for all the responses, I resolved the problem from your help. All I did was change 5/9 to 5.0/9.0 and it resolved it. Since I am just beginning with C++ and taking a college course I cant stray from the things I have been taught. I can't use any advanced coding due to not learning it yet, and the professor would be kind of concerned that I'd be cheating. Also, dont get so worked up with people on this forum. The purpose is just help people and discuss. We are all learning still, right?