Hello, I just wrote my first code with a while loop and I am getting strange results. The program is supposed to give a Fahrenheit (TF) to Celsius (TC) conversion for every 5 degrees Fahrenheit increases between 0 and 100. It increases the value of TF just fine but in the results for TC it will only give the value -0 or 0. The odd thing is it changes from -0 to 0 when TF is high enough to make TC be positive. So I can't figure out why it won't let TC be any other number than 0


using namespace std;

int main()
	// Declare and initialize objects.
	double TF, TC;

	// Introduction.
	cout << "Fahrenheit -> Celsius degree conversion table." << endl;

	// Table creation.
	while(TF<= 100)
	{ TC= (TF-32)*(5/9);
	  cout << TF << " Becomes " << TC << endl;
	  TF= TF+5; }

Thank you for your time.

Edited by WaltP: Added CODE Tags -- please use them.

6 Years
Discussion Span
Last Post by Craftknight

5/9 is an integer division, which gives the value of 0. Change one or both of them to have a decimal (5.0/9, 5/9.0, or 5.0/9.0) which will perform the calculation with doubles.


Well that explains it, I thought it was something silly like that, but I couldn't figure it out. Thank you so much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.