Did I create this function correctly for the first part of the programming assignment?

Write a program to read the coefficients of a series of quadratic equations from a text file and print the associated roots, or appropriate errors if there are no real roots, to another text file. The coefficients of a quadratic are the a, b and c of an expression of the form ax2 + bx + c, and the roots are the values of x that make the value of the expression 0.

If a == 0, the formula describes a line, and you may ignore any roots (just say it isn’t a quadratic and has no solutions), and if (b2-4ac) < 0 it has only complex roots.

Part 1: Write a function to read one set of values from the file, using reference parameters to get the values out of the function and the function’s return value to indicate whether or not the function was able to correctly read three values. The data error you must deal with here is too few values on the line, e.g., the line has an a value only and no b or c. You may assume that the last line in the file is the only one with an error (if any error exists), and your function should return an error code to make the program stop processing after this error. This restriction allows you to use stream extraction to read the file, rather than reading lines and parsing them yourself. If you want to try doing this, that’s O.K., but get it working the easy way first.

Note: You may not use any global variables in this program. Your variables must be declared appropriately within the functions where needed; and passed to other functions as either reference or value parameters as appropriate.

Each correct input line will comprise three real values of the form
[optional sign][digits][decimal point][digits], or
[optional sign][digits] if integer.
The last input line might have fewer values. For example, your data file might look like this:

1 1 1
1.2 -2.3 0.4
-2 -3 -4
+0 -2 8.85
2.345 (error — only one data point)

These data are in the file quadratic1.txt

Here is my solution for the first part of the program:

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
30
#include<iostream>
#include<fstream>
using namespace std;

// Read each set of values from the input file
double quadValues(double &value)
{
	ifstream inputFile;
	inputFile.open("quadratic1.txt");
	inputFile >> value;
	if(value >= 3) // Indicates whether function read three values
	{
		cout << value << endl;	  	  
		return value; 
	}
	else // Print error message
	{
		cout << "You must have values for a, b, and c" << endl;
		return 0;
	}
	inputFile.close();
}

int main()
{
	double value;
	quadValues(value);
	return 0;
}
Your "solution for the first part of the problem" makes absolutely no sense to me and seems to have nothing to do with the first part of the problem. What were you trying to do? For starters, it seems like you're confused about how to use different aspects of C++ appropriately.
Last edited on
Topic archived. No new replies allowed.