i started doing a program that my professor assigned and realized that i can't use arrays because i dont know how many points there are going to be in the file that the user decides to open. i'm stuck on how to keep track and store all of the coordinates without an array.
here is the question:
2. You will write a program which calculates the “best fit” line for a number
of data points.
(a) Get the points from a user-specified file (the user should be able to
choose any filename). Each line of the file will contain an x-coordinate
and a y-coordinate, but you do not know how many lines are in the
file. (You will use: while(inFile.good()) for this purpose)
(b) Your program will then calculate the equation for the best-fit line
and display it to the monitor.
Because you do not know how many points the file will contain ahead of
time, you can’t use arrays in this problem (because you won’t know how
big to make them). Instead, you will need to keep track of the number of
points in the file, and all the necessary sums as you input each point.
here is what i have so far:
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
|
#include<iostream>
#include<cstdlib>
#include<fstream>
using namespace std;
int main()
{
ifstream inFile;
cout << "Enter a file name containing the points: ";
string inputFilename;
cin >> inputFilename;
inFile.open(inputFilename.c_str());
int n, x, y;
char p;
while(inFile.good())
{
inFile >> n >> x >> p >> y;
}
system("PAUSE");
return 0;
}
|
thanks!!!