May 28, 2016 at 1:23pm UTC
Hey, whats wrong with this? Why when I use ofstream for writing the results in file the program does not work? I mean the line "write the number" just keeps on looping
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#include<iostream>
#include <iomanip>
#include<fstream>
using namespace std;
int main(){
float x, sum=0,numofnumbersentered=0; // x- number
float avg; //average
ofstream fr ("results.txt" );
cout<<"write the number" <<endl;
fr<<x;
while (x!=0){
sum=sum+x;
numofnumbersentered++;
cout<<"write another number" <<endl;
fr<<x;
}
avg=sum/numofnumbersentered;
fr<<"average:" <<fixed<<setprecision(1)<<avg;
fr.close();
return 0;
}
Last edited on May 28, 2016 at 1:30pm UTC
May 28, 2016 at 1:30pm UTC
Is line 15 meant to be cin >> x;
?
May 28, 2016 at 1:34pm UTC
And is line 20 meant to be cin >> x
also? Right now x
doesn't change inside the loop so the loop goes forever.