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;

}   
4

2 に答える 2

1

コードを無限ループで実行し、-9999 が表示された場合は break を使用してループを終了します。

#include <iostream>
using namespace std;
int main ()
{

int fahr=0,cent=0,count=0,fav=0;

while (true)
{
    count ++;       
    cout<<"Input the fahrenheit temp to be converted to centigrade or enter -9999 "<<endl;
    cin>>fahr;

    if (fahr == -9999)
       break;

    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;

} 
于 2012-02-09T05:14:09.397 に答える
0

おそらく、後にもう1行追加する必要があります

cout<<"The cent equivalent "<<cent<<endl;

追加:

fav += cent;

変更する

fav=(float)(fav+fahr)/count;

に:

fav=(float)fav/count;
于 2012-02-09T05:20:13.580 に答える