C++ Temperature Converting -


i have problem in while loop. program reads quit (-999) line after reading fahrenheit temperature. in beginning didn't have problem until trying make print correct answer average , ruined everything.

here code:

#include<iostream> #include<iomanip> #include<fstream> using namespace std; void main()  {     float fahr =0 , cent = 0, fsum = 0, csum = 0;     float favg = 0;     float cavg = 0;     int temp_cnt = 0;     ofstream fout;     fout.open("temps.dat");     fout << setw(25) << "fahrenheit" << setw(20) << "centigrade" << endl;     fout.setf(ios::fixed);     fout.setf(ios::showpoint);     fout.precision(1);     while (fahr != -999)     {           cout << "enter fahrenheit temperature enter. -999 toquit"<< endl;     temp_cnt++;     cin >> fahr;     fsum += fahr;     csum += cent;      cent = float((5.0 / 9.0))*float((fahr - 32));     fout << setw(25) << fahr << setw(20) << cent << endl;     csum = csum + cent; }  fout << endl; fout << setw(15) << "average:"; favg =float ((fsum / temp_cnt)); cavg = float ((csum / temp_cnt)); fout << setw(10) << favg << setw(20) << cavg << endl; return ; } 

and output in temp.dat file:

           fahrenheit          centigrade                  86.0                30.0                  91.0                32.8                  87.4                30.8                 100.3                37.9                  98.6                37.0                  77.0                25.0                  83.7                28.7                -999.0              -572.8     average:     -46.9               -16.0 

after line:

cin >> fahr; 

add:

if(fahr==-999)     break; 

hope helps. luck!


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -