Trying to learn C++
Cannot seem to figure out what i am doing wrong. Code is in the files 1 .h and 2 .cpp files
H0-QuadraticFunction.h
struct cmplxNumber{
double dReal, dImaginary;
} ;
bool ComputeRoots( double dCoefA, double dCoefB, double dCoefC,
cmplxNumber & Root1, cmplxNumber & Root2);
ostream & PrintComplex( ostream & outDevice, cmplxNumber Root );
H0-QuadraticFunction.cpp
#include "H0-QuadraticFunction.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <ostream>
using namespace std;
int main()
{
// Need some variable storage for parameters
double dCoefA = 0.0, dCoefB = 0.0, dCoefC = 0.0;
cout << "Enter coefficient A: ";
cin >> dCoefA;
cout << "Enter coefficient B: ";
cin >> dCoefB;
cout << "Enter coefficient C: ";
cin >> dCoefC;
cmplxNumber Root1, Root2;
ComputeRoots( dCoefA, dCoefB, dCoefC, Root2, Root1 );
cout << "\n\nThe first root is " << Root1.dReal;
if( Root1.dImaginary < 0.0 ) {
cout << " - " << abs(Root1.dImaginary) << " j" << endl;
}
else if( Root1.dImaginary > 0.0 ) {
cout << " + " << Root1.dImaginary << " j" << endl;
}
else
cout << endl;
cout << "\n\nThe second root is ";
PrintComplex( cout, Root2 );
system("pause");
return 0;
}
H0-QuadraticFunction-Helper.cpp
bool ComputeRoots( double dCoefA, double dCoefB, double dCoefC,
cmplxNumber & Root1, cmplxNumber & Root2)
{
double dInter1, dInter2;
dInter1 = dCoefB * dCoefB; // get the square of B
dInter2 = 4.0 * dCoefA * dCoefC;
if(dInter1 < dInter2 ) {
Root1.dImaginary = pow(abs(dInter1 - dInter2), 0.5)/ (2.0 * dCoefA);
Root2.dImaginary = -pow(abs(dInter1 - dInter2), 0.5)/ (2.0 * dCoefA);
Root1.dReal = Root2.dReal = -dCoefB / (2.0 * dCoefA);
} else {
Root1.dReal = (-dCoefB + pow(dInter1 - dInter2, 0.5))/ (2.0 * dCoefA);
Root2.dReal = (-dCoefB - pow(dInter1 - dInter2, 0.5))/ (2.0 * dCoefA);
}
return true;
}
// declaration of function using 'things' from the stream devices is
// fairly exacting. MUST use references to the device variables.
ostream & PrintComplex( ostream & outDevice, cmplxNumber Root )
{
outDevice << Root.dReal;
if( Root.dImaginary < 0.0 ) {
outDevice << " - " << abs(Root.dImaginary) << " j" << endl;
}
else if( Root.dImaginary > 0.0 ) {
outDevice << " + " << Root.dImaginary << " j" << endl;
}
else
cout << endl;
return outDevice;
}
Include the complex number structure's header file in H0-QuadraticFunction-Helper.cpp. It needs to know what a cmplxNumber is.