Reputation: 278
So I've got this simple piece of code. What it's supposed to do is calculate centigrade from Fahrenheit. For some reason, however, it keeps resetting the value of cent to 0. Can anyone please explain why it's doing this? I'm sure I'm just missing something small and stupid.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main (void)
{
ofstream fout("temps.dat");
float fahr=0, avgcent=0, avgfahr=0, totalcent=0, totalfahr=0, gettemp=0;
double cent = ((5/9) * (fahr - 32));
const int gatekeeper=-99999;
fout << setw(25) << "Fahrenheit" << setw(20) << "Centigrade" << endl << endl;
fout.setf(ios :: fixed);
fout.setf(ios :: showpoint);
cout << "Please input Fahrenheit temp (-99999 to quit)" << endl;
cin >> fahr;
while (fahr != gatekeeper)
{
totalfahr = totalfahr + fahr;
totalcent = cent + totalcent;
cout << cent;
cin >> fahr;
}//End of while (gettemp != gatekeeper)
}//End of int main (void) for HW2
Upvotes: 2
Views: 873
Reputation: 6793
In your line double cent = ((5/9) * (fahr - 32));
, the expression (5/9)
resolves to 0
because it uses integer division. Replace it with (5.0/9.0)
and you should get the correct result.
Upvotes: 8