0
现在的代码,哨兵正在计入平均水平。任何指针如何打破循环,而不包括哨兵?我们如何打破循环而不将标记添加到平均值?
#include <iostream>
using namespace std;
int main()
{
int fahr=0,cent=0,count=0,fav=0;
while (fahr!=-9999)
{
count ++;
cout<<"Input the fahrenheit temp to be converted to centigrade or enter -9999 "<<endl;
cin>>fahr;
cent=(float)(5./9.)*(fahr-32);
cout<<"The inputed fahr "<<fahr<<endl;
cout<<"The cent equivalent "<<cent<<endl;
}
fav=(float)(fav+fahr)/count;
cout<<"Average"<<fav<<endl;
return 0;
}
您的代码不计算平均值。每个循环都需要一些累加器,比如'total + = fahr'。那么你的平均值应该是“总数/总数”。另外,如果结果存储为“int”,则不需要将总数转换为“float”。无论如何,你最终会得到与整数除法相同的结果。 – japreiss 2012-02-09 05:26:04