May 5th, 2011, 06:43 PM
EOF() function is re-reading a line of data
I have to write a program to calculate the least squares line from a set of data. I named the file "data.txt" and these are the contents:
and this is my program so far:
I havent calculated the line yet, I am stuck with reading the data from the file. From what I've read, I gathered that eof() doesn't become true until it reads past the end of the file. I read that you should use the get() function to force the program into reading the next line, triggering the eof to switch, but no matter which was I organize my while loop, I can't get it to run properly. I end up with 6 data points instead of five, the extra data point is the last data point which is re-entered into the algebra, giving me incorrect data.
using namespace std;
int numpoints = 0;
double x=0, y=0, sumx=0, avgx=0, sumy=0, avgy=0, sumxy=0, sumxsquared=0, sumysquared=0;
double m=0, b=0, r=0, first=0,second=0,third=0,fourth=0;
cout << "There was an error opening the file.\n";
while (! inStream.eof())
inStream >> x >> y;
sumx += x;
sumy += y;
sumxy += (x*y);
sumxsquared += (x*x);
sumysquared += (y*y);
cout << "Sum X=" << sumx <<"\n"
<< "Sum Y=" << sumy <<"\n"
<< "Sum XY=" << sumxy <<"\n"
<< "Sum X^2=" << sumxsquared <<"\n"
<< "Sum Y^2=" << sumysquared <<"\n"
<< "Number of points=" << numpoints << "\n";
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!