所以我有这么简单的代码.它应该做的是从华氏温度计算摄氏度.然而,出于某种原因,它会将分值重置为0.任何人都可以解释为什么这样做?我敢肯定我只是错过了一些小而愚蠢的东西.
#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
Run Code Online (Sandbox Code Playgroud)
在你的行中double cent = ((5/9) * (fahr - 32));,表达式(5/9)解析为0因为它使用整数除法.替换它,(5.0/9.0)你应该得到正确的结果.