### Solved.

Solved.
Last edited on
closed account (D80DSL3A)
This function determines what the output looks like:
 ``53545556`` ``````std::ostream& operator<<(std::ostream& lhs, const Complex& rhs) // << operator. { return lhs << '(' << rhs.r << " + " << rhs.i << "i)"; }``````

You need to write the logic into this function so it gives the format desired.
For example, to make it print 2 - 3i instead of 2 + -3i:
 ``12345678910`` ``````std::ostream& operator<<(std::ostream& lhs, const Complex& rhs) // << operator. { lhs << '(' << rhs.r; if( rhs.i >= 0 ) lhs << " + " << rhs.i << "i)"; else lhs << " - " << -rhs.i << "i)"; return lhs; }``````

and so on to cover the cases when r or i are zero.
This part is your main problem:

 ``12345`` ``````std::ostream& operator<<(std::ostream& lhs, const Complex& rhs) // << operator. { return lhs << '(' << rhs.r << " + " << rhs.i << "i)"; } ``````

You're forcing the output to only be one thing. You could fix it with a few if 's. Such as:

 ``12345678`` `````` if (rhs.i = 0) return lhs << '(' << rhs.r << ')'; if (rhs.i < 0) return lhs << '(' << rhs.r << rhs.i << "i)"; if (rhs.i >0) return lhs << '(' << rhs.r << '+' << rhs.i << "i)"; ``````

As for getting the proper output on the right-hand side of the equation you will probably need to create a separate object (I'm not sure of the proper terminology for C++, and its late) for outputting the right-hand side of the equation.

I'm pretty sure that's your main problem anyway. I could be wrong.
Topic archived. No new replies allowed.