I am trying to write my first while loop. It's supposed to start with a Celsius temperature of 100, then calculate the Fahrenheit equivalent, then subtract one from the C temp until the Fahrenheit and Celsius are equal (which is at -40).
I have absolutely no idea where I'm going wrong. I've spent all day on the internet trying to figure it out. I'm sure it's just a dumb mistake. Help, please?
#include <iostream>
usingnamespace std;
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
int cels; // degrees celsius
int fahr; // degrees fahrenheit
cout << "This program determines which temperature\n";
cout << "is the same in both Celsius and Fahrenheit.\n";
cels = 100;
//fahr = (9/5) * cels + 32;
while (cels != fahr)
{
fahr = (9/5) * cels + 32;
cels = cels - 1;
}
cout << "The temperature " << fahr << " is the same in both Fahrenheit and Celsius.\n";
cout << "Goodbye.\n";
return (0);
}
(Also, sorry if the formatting is wrong!! Any help is greatly appreciated.)
Formatting is fine, actually, except that one return at the end but that's forgivable compared to other things I've seen.
Now then, what is going wrong? I would warn you now that, if you divide 9 by 5, you get 1 because those are both integers. If you divide 9.0 by 5 or 9 by 5.0 you get 1.8.
#include <iostream>
usingnamespace std;
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
int cels; // degrees celsius
int fahr; // degrees fahrenheit
cout << "This program determines which temperature\n";
cout << "is the same in both Celsius and Fahrenheit.\n";
cels = 100;
fahr = (9.0/5.0) * cels + 32;
while (cels != fahr)
{
cels = cels - 1;
fahr = (9/5) * cels + 32;
}
cout << "The temperature " << fahr << " is the same in both Fahrenheit and Celsius.\n";
cout << "Goodbye.\n";
return (0);
}
But the output just ends up being "This program determines which temperature is the same in both Celsius and Fahrenheit." Is there some reason that cels never equals fahr exactly? Why does the program keep getting stuck in the loop?