Write a program that displays the table of Fahrenheit - Celsius temperatures.

Below is my code, the problem with it is that the celsius column is all zeros,please correct my mistake, thank you:

#include "stdafx.h"
#include "genlib.h"
#include "simpio.h"
#include "math.h"
int _tmain(int argc, _TCHAR* argv[])
	 int i;
	 double c;
	 printf("Fahrenheit       Celsius\n");
	 for (i=0;i<=0;i+=20)
		  printf("%d                %f\n",i,c);
	 for (i=20;i<=80;i+=20)
		  printf("%d               %f\n",i,c);
	 for (i=100;i<=200;i+=20)
		  printf("%d              %f\n",i,c);


I tried using %g instead of %f, but it's still all zeros.


The output should appear as follows:

Fahrenheit       Celsius
0                -17.8 
20               -6.7
40               4.4 
60               15.6
80               26.7
100              37.8
120              48.9
140              60.0
160              71.1
180              82.2
200              93.3
Press any key to continue_

The output that my program has:

Fahrenheit       Celsius
0                0
20               0
40               0 
60               0
80               0
100              0
120              0
140              0
160              0
180              0
200              0
Press any key to continue_


I tried using %g instead of %f, but it's still all zeros.

the problem with it is that the celsius column is all zeros,please correct my mistake, thank you

I'm not 100% sure why (5/9) is having problems displaying as a double but that is why you are getting a bunch of zeros.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
    float cel = 0;
	cout << setprecision(1) << fixed; //sets 1 decimal place (fixed makes it decimals instead of digits)
    cout << "Fahrenheit\tCelsius" << endl;
	for( int i = 0; i <= 200; i += 20 ) //not sure why you had a bunch of for loops when you can use 1
		cel = (5.0/9.0)*double(i-32); //could cast 5 and 9 as doubles or just make them doubles like I did
		cout << i << "\t\t" << cel << endl;
    return 0;

I believe that sfuo is correct that the problem is you lack of cast to a double

The reason that you had the original problem was that 5/9 is integer/integer. In
integer arithmetic you don't change type and an integer is returned: e.g 5/9 is
zero. Additionally 8/9 is zero, and 11/9 is one , as is 17/9. Hopefully, you get the idea.

You can quickly move something into double format e.g. (5.0/9.0), (the default for a decimal number even with a zero in the decimal place is double).

Any simple binary operation involving one double and one integer, the integer is upcast to a a double and a double is evaluated. e.g. 5/9.0 and 5.0/9 are both doubles. However, 8/6.0 - 4/3 is not zero [it is 0.3333] because the 4/3 is done before - and produces an integer (1). The upcast is only for each pair in the expression and the expression is reduced.

So you can re-write sfou's cel = (5.0/9.0)*double(i-32); as cel=5/9.0*(i-32); . That is because (i-32) returns an integer.
then 5/9.0 must be done BEFORE * (since divide take priority). so we are left
with (double)*(integer) and from the rules above we have an double.

There is one final problem on the matter. In C++, there are several floating point numbers , float, double, long double. Which depending on the compiler/platform are typically on a PC. 32bit, 64bit and 128bit, but can be different. [You can find out with sizeof(double) etc.] But this can lead to this kind of problem due to the inaccuracy of computational representation.

float a(5.0);
float b(9.0);  
std::cout<<"Near zero = "<<a/b-5.0/9.0<<std::endl;
std::cout<<"Zero =="<<a/b-5.0f/9.0f<<std::endl;

As you can see since 5/9 cannot be exactly represented in binary, this expression is type dependent.

Bit long I know, but I figures sfou and you might have wanted to know.

thank you very much. You guys seem to know way more than my online course instructor does.