Quadratic Formula Help

I am trying to create a quadratic equation program that also places the output in a .txt file. This is my program right now. It compiles but it places the answer in the form <2.4, 0> <0,5.4> and i need the answer to look like 2.4 5.4
Also I cannot figure out how to put the answers in order from least to greatest because when i try and put in another if statement such as if ( x1 << x2) cout << x1, x2; except it underlines the << and gives me the error message "NO OPERANDS MATCH THIS OPERATOR"....how can i fix this problem?? :( Thanks!


#include <iostream>
#include <fstream>
#include <cmath>
#include <complex>
#include <string>

using namespace std;

int main()
{
ofstream program4;
program4.open ("program4.txt");

complex <double> a, b, c, x1, x2;

cout << "A:" ;
cin >> a;
cout << "B:" ;
cin >> b;
cout << "C:" ;
cin >> c;

if ( a==0.0 || b == 0.0 || c == 0.0 )
cout << "NO REAL ROOTS" ;

else
x1 = (-b + sqrt(b * b - 4.0 * a * c )) / (2.0 * a);
x2 = (-b - sqrt(b * b - 4.0 * a * c )) / (2.0 * a);

program4 << x1 << fixed <<","<< x2<< fixed ;

program4.close();

cout << x1 << x2 << endl;

return 0;
}
You are using complex numbers, so the default output for a number should look like (a,b), where a is the real and b is the imaginary part. If you want some other output format, you can access the a real and imaginary part using real() and imag() member functions.[1]

A quadratic equation doesn't have real root(s) iff b^2-4ac < 0.

The less than comparison operator is simply <, not <<. Also note that complex numbers doesn't have comparison operators defined.

[1] http://cplusplus.com/reference/std/complex/complex/
Last edited on
Ohh I see. Okay i can leave the answer in that format but how do i define the comparison operators then? I thought that including c math would automatically include all operators
There is no obvious way to sort points in (the complex) plain. Which is bigger (1,2) or (0,3) ?
You can compare the real parts1, or the imaginary parts2, or the distance from the origin3 etc...

1.: x.real() < y.real()
2.: x.imag() < y.imag()
3.: std::abs(x) < std::abs(y)
That Worked! Thanks! Okay so this is my code now and the only thing thats not making sense is how i make the no real roots statement apply to program4.txt also?

#include <iostream>
#include <fstream>
#include <cmath>
#include <complex>
#include <string>

using namespace std;

int main()
{
ofstream program4;
program4.open ("program4.txt");

complex <double> x1, x2;
double a, b, c;


cin >> a;

cin >> b;

cin >> c;

x1 = (-b + sqrt(b * b - 4.0 * a * c )) / (2.0 * a);
x2 = (-b - sqrt(b * b - 4.0 * a * c )) / (2.0 * a);

program4 << x1.real() << ", "<< x2.real();

program4.close();

if ( x1 == x2)
cout << x1.real() ;
if ( x1.real() < x2.real())
cout << x1.real() << ", " << x2.real() ;
if ( b < 4.0 * a * c)
cout << "NO REAL ROOTS" << endl;
else
cout << x2.real() << ", " << x1.real() ;

return 0;

}
Last edited on
Also, I need the same statement " if (x1.real() < x2.real()) cout << x1 << x2 " to apply to the answer that I read out to program 4 but it messes up when i try and put an if statement in after program 4 << so where would i be able to place that?
Topic archived. No new replies allowed.