-
Help with C++ Program
This code is supposed to calculate and print out the temperature where the temperature in celsius is the same in Fahrenheit. So it should print -40 but instead it prints nothing. Please help.
Code:
#include<iostream>
using namespace std;
int main()
{int celsius=100, fahrenheit;
do {
fahrenheit=9/5*celsius+32;
celsius--;}
while(celsius+1!=fahrenheit);
cout<<"Celsius and fahrenheit are the same at "<<fahrenheit;
return 0;
}
-
9/5 is 1 in C++
You should reorganise that expression so that the multiplication is executed before the division
That's horrible formatting btw, never put the closing brace on the end of a line of code.
Add some whitespace too.
Check the FAQ for why your console disappears so quickly.
-
Indent, format. 9/5 is integer divsision; thus it yields one. If you do 9.0/5, you should see that you'll get some different answers.
-
Hmm, and then after you convert to floating point there is the issue of floating point equality.
abs( fahrenheit - celsius ) < 0.0001
Or however close you want to be, but not exact.