-
May 5th, 2011, 05:43 PM
#1
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:
Code:
-0.9 -1.1
-0.01 0.23
1.1 1.3
2.0 2.0
-2.0 -1.85
and this is my program so far:
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
using std::ifstream;
using std::ofstream;
int main()
{
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;
char next;
ifstream inStream;
inStream.open("data.txt");
if (inStream.fail())
{
cout << "There was an error opening the file.\n";
return 0;
}
while (! inStream.eof())
{
inStream >> x >> y;
sumx += x;
sumy += y;
sumxy += (x*y);
sumxsquared += (x*x);
sumysquared += (y*y);
numpoints ++;
inStream.get(next);
}
inStream.close();
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";
return 0;
}
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|