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

#include<iostream>
#include<cmath>

using namespace std;

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

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

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

Thank you for your time.

Recommended Answers

All 4 Replies

5/9 = 0 so you are multiplying by 0.

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.

You are practically dividing by zero !! (5/9).. try (5.0/9.0)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.