
May 7th, 2013, 07:09 PM
#1
Celcius Conversion Loop
So everything seems to be working fine here, but after the first calculation that converts 0C to 32F, it just adds increments of 1 to each Fahrenheit. Why isn't the formula being repeated for each increasing value of Celcius? Help would be greatly appreciated, as I have spent hours trying to figure this out.
Code:
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
double c;
cout << setw(4) << "Celcius" << setw(7) << "" << "Fahrenheit" << endl;
for (c = 0; c <= 20; c++)
{
double f = (9/5) * c + 32;
cout << setw(4) << c << setw(7) << " " << " " << " " << " " << f << endl;
}
return 0;
}

May 7th, 2013, 07:50 PM
#2
Re: Celcius Conversion Loop
Originally Posted by bertonius
So everything seems to be working fine here, but after the first calculation that converts 0C to 32F, it just adds increments of 1 to each Fahrenheit. Why isn't the formula being repeated for each increasing value of Celcius? Help would be greatly appreciated, as I have spent hours trying to figure this out.
What do you think this gives you?
When you divide an integer by an integer, the result is an integer.
Regards,
Paul McKenzie

May 7th, 2013, 08:41 PM
#3
Re: Celcius Conversion Loop
Thank you, Paul. I changed the formula to (1.8 * c) + 32 instead, and it is working. Here's what it looks like now:
Code:
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int c;
cout << setw(4) << "Celcius" << setw(7) << "" << "Fahrenheit" << endl;
for (c = 0; c <= 20; c++)
{
int f = (1.8 * c) + 32;
cout << setw(4) << c << setw(7) << " " << " " << " " << " " << f << endl;
}
return 0;
}

May 7th, 2013, 09:57 PM
#4
Re: Celcius Conversion Loop
Originally Posted by bertonius
Thank you, Paul. I changed the formula to (1.8 * c) + 32 instead, and it is working.
You could have easily just done this:
The numerator and denominator are both floatingpoint (double), so the result will be a double instead of having the truncation done as in (9/5).
Regards,
Paul McKenzie
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
This is a CodeGuru survey question.
Featured
