-
March 10th, 2009, 05:03 PM
#1
Simple Assignment Help
New guy here. I'm barely in my second C++ class, and we are doing some review and I cannot figure out why this won't give me what I want. The problem is to create a function to convert Fahrenheit to Celsius. It's to be enclosed in a loop so that my output is a list that shows what 1-20 degrees fahrenheit is in Celcius. This is my code:
// Celcius Temperature Table
#include <iostream>
#include <iomanip>
using namespace std;
double celsius(double);
int main()
{
double fah, cel;
cout << fixed << showpoint << setprecision(1);
cout << "Fahrenheit\tCelcius\n";
cout << "----------------------\n";
for (fah = 0; fah <= 20; fah++)
{
cel = celsius(fah);
cout << fah << "\t" << cel << endl;
}
return 0;
}
double celsius(double fah)
{
return ((5 / 9) * (fah - 32));
}
I can't see what is wrong, but whenever I compile it, it loops like it should and lists Fahrenheit 1-20, but in the celcius column every value is 0.0. Can someone help me out, or at least give me a hint about what I'm doing wrong?
-
March 10th, 2009, 05:04 PM
#2
Re: Simple Assignment Help
Hint: In C(++), 5/9 is not the same as 5.0/9.0.
-
March 10th, 2009, 05:10 PM
#3
Re: Simple Assignment Help
Oh man. That was it. I've been going crazy trying to find out what was wrong. I definitely won't forget this again. Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|