Need help with reading from file line by line.

I have an assignment to create a quadratic formula program. I need to read from a file and calculate the roots.
example of the txt file:
1 -2 3
1 2 3
5 4 2
2 1
My prof told me to extract the variable a, b, and c separately. I need to be able to tell if the last line in the file is missing b or c.
I also have no idea how to loop the main function so the it read the data, processed it, and print result then proceed to read the next values..

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
29
 int main()
{
	double a , b, c;
	double root1, root2, oneSol;
	int result, error;

	error = readInput(a, b, c);
	result = calculation();
	print();
	return 0;
}

int readInput(double &a, double &b, double &c)
{
	ifstream inFile;
	inFile.open("number.txt");
	inFile >> a >> b >> c;
	
	if (a == 0)
	{
		return -1;
	}
	if (b == 0 && c == 0)
	{
		return -2;
	}
	return 0;
}
To read a file line by line, you could read the entire line as a string.
Then use a stringstream to read the variables within that line.

1
2
3
4
    double a, b, c;
    std::string line = "1 -2 3";  // example line
    std::istringstream ss(line);  // make a stream
    ss >> a >> b >> c;            // read from the stream 



There's an example of reading a file a line at a time using getline() under the heading Text files in the tutorial:
http://www.cplusplus.com/doc/tutorial/files/

Topic archived. No new replies allowed.