Hey, had a questionabout some c++ code... using this exact function.... void roo
ID: 640826 • Letter: H
Question
Hey, had a questionabout some c++ code... using this exact function....
void rootsPrint(double a, double b, double c, double root1, double root2, bool $
cout << "Quadratic equation with the following coefficients:" << endl;
cout << "a: " << a << "; b: " << b << "; c: " << c << endl;
if (roots) {
if (a == 0) { //if linear print only one root
cout << "Has the following roots" << endl;
cout << "Root1: " << root1 << endl << endl;
}
else //else print both roots for quadratic equation
cout << "has the following roots" << endl;
cout << "Root1: " << root1 << "; Root2: " << root2 << e$
}
else { // roots false prints no roots
cout << "has no roots in the real domain." << endl << endl;
}
}
im curious how to change it around so that the function rootsPrint() should write the existing roots not to the screen but to a file named rootResults.dat in this format:
Quadratic equation with the following coefficients:
a: <value>; b: <value>; c: <value>
has the following roots
Root1: <value>; Root2: <value>;
and it should display a message to the screen, in case roots do NOT exist, in this format:
Quadratic equation with the following coefficients:
a: <value>; b: <value>; c: <value>
has no roots in the real domain.
Thanks!!
Explanation / Answer
void rootsPrint(double a, double b, double c, double root1, double root2, bool roots) {
ofstream out;
out.open("rootResults.dat");
if (roots) {
out << "Quadratic equation with the following coefficients:" << endl;
out << "a: " << a << "; b: " << b << "; c: " << c << endl;
if (a == 0) { //if linear input only one root to the file
out << "Has the following roots" << endl;
out << "Root1: " << root1 << endl << endl;
} else{
//else input both roots for quadratic equation to the file
out << "has the following roots" << endl;
out << "Root1: " << root1 << "; Root2: " << root2 << endl;
}
} else { // roots false prints no roots
cout << "Quadratic equation with the following coefficients:" << endl;
cout << "a: " << a << "; b: " << b << "; c: " << c << endl;
cout << "has no roots in the real domain." << endl << endl;
}
}