Как нам разорвать цикл, не добавляя дозорного к среднему? - PullRequest
0 голосов
/ 09 февраля 2012

Поскольку код остается в силе, дозорный включается в расчет среднего.Любые указатели о том, как разорвать петлю, не включая стража?

#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;

}   

Ответы [ 2 ]

1 голос
/ 09 февраля 2012

Заставьте код выполняться в бесконечном цикле и используйте разрыв, чтобы завершить цикл, если вы видите -9999.

#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;

} 
0 голосов
/ 09 февраля 2012

Возможно, вам нужно добавить еще одну строку после

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

, добавить:

fav += cent;

и изменить

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

на:

fav=(float)fav/count;
...