# quadratic root and imaginary result

• 12-12-2009
drkidd22
Hello,

I have the following program, but the issue I'm having that whenever one of the roots is imaginary both solutions become zero. Any help with this will be appreciated.

Code:

```#include <iostream> #include <iomanip> // this header file identifies all of the special mathematical functions. #include <cmath> using namespace std; // Version 1.2 Adds functions for Quadratic roots of complex value //    This is an incomplete version for students to work with // Version 1.1 Removed standard method calls and implemented operator overloading // Version 1.0 Initial complex value declaration struct ComplexStruc {   double real, imaginary; } ; class myComplex {   double real, imaginary; public:   myComplex( double dReal = 0.0, double dImaginary = 0.0 );   double getReal( void ) { return real; };   double getImaginary( void ) { return imaginary; };   void setReal( double dR ) { real = dR; };   void setImaginary( double dI ) { imaginary = dI; };   friend ostream& operator<<(ostream& osLeft, const myComplex& right);   const myComplex operator +( const myComplex& right ) const;   const myComplex operator -( const myComplex& right ) const;   const myComplex operator *( const myComplex& right ) const; } ; double QuadraticRoot1( double dA, double dB, double dC ) {   double dRoot = 0;   double dIntermediate1 = 0.0, dIntermediate2 = 0.0;   if( (dIntermediate1 = 4.0 * dA * dC) < (dIntermediate2 = dB * dB) ) {       dRoot = (-dB - pow(dIntermediate2 - dIntermediate1, 0.5)) / (2.0 * dA);   }   else {       // value is complex   }   return dRoot; } double QuadraticRoot2( double dA, double dB, double dC ) {   double dRoot= 0;   double dIntermediate1 = 0.0, dIntermediate2 = 0.0;   if( (dIntermediate1 = 4.0 * dA * dC) < (dIntermediate2 = dB * dB) ) {       dRoot = (-dB + pow(dIntermediate2 - dIntermediate1, 0.5)) / (2.0 * dA);   }   else {       // value is complex   }   return dRoot; } int main() {   double dA, dB, dC ;   myComplex mcA(1.2, 1.3), mcB(2.1, 2.3), mcC(0.0, 0.0);   cout << endl << endl << endl << "Using overloaded operators" << endl << endl;   cout << "The original value of parameter C is zero." << endl;   cout << "Only the real component value of zero shows, " << mcC << endl << endl;   mcC = (mcA + mcB);   cout << "Addition, " << mcC << endl << endl ;   cout << "Multiplication and subtraction (A*B)-B, " << (mcA*mcB)-mcB << endl << endl;   do {       cout << "Quadratic solution, input coefficients A, B then C "         << endl << "enter all zeros to stop " << endl ;       cin >> dA >> dB >> dC ;       if( dA == 0 && dB == 0 && dC == 0 )         break;       cout << "Root 1 = " << QuadraticRoot1( dA, dB, dC ) << endl << endl ;       cout << "Root 2 = " << QuadraticRoot2( dA, dB, dC ) << endl << endl ;   } while( true );   system( "pause" );   return 0; } myComplex::myComplex(double dReal, double dImaginary) {   real = dReal;   imaginary = dImaginary;   return; } ostream& operator<<(ostream& osLeft, const myComplex& rhv) {   osLeft << fixed << setprecision(3) << rhv.real ;   if( rhv.imaginary < 0 )       osLeft << " - " ;   else if( rhv.imaginary > 0 )       osLeft << " + " ;   else       return osLeft;   osLeft << abs(rhv.imaginary) << " j" ;   return osLeft; } const myComplex myComplex::operator +( const myComplex& rhv )const {   myComplex temp(0,0);   temp.real = real + rhv.real;   temp.imaginary = imaginary + rhv.imaginary;   return temp; } const myComplex myComplex::operator -( const myComplex& rhv )const {   myComplex temp(0,0);   temp.real = real - rhv.real;   temp.imaginary = imaginary - rhv.imaginary;   return temp; } const myComplex myComplex::operator *( const myComplex& rhv )const {   myComplex temp(0,0);   temp.real = (real * rhv.real)  + (imaginary * rhv.imaginary);   temp.imaginary = (real * rhv.imaginary)  + (imaginary * rhv.real);   return temp; }```
• 12-12-2009
Epy
This would probably be better suited for the C++ forum
• 12-12-2009
laserlight
Agreed, and moved.
• 12-12-2009
zalezog
Code:

```double QuadraticRoot1( double dA, double dB, double dC ) {     double dRoot = 0; . .   else {       // value is complex   }   return dRoot; } double QuadraticRoot2( double dA, double dB, double dC ) {   double dRoot= 0;   .   .   else {       // value is complex   }   return dRoot; }```
So,you have nothing in the else block therefore initial value of dRoot is returned.
ie. 0