Variables not passing too function, help!

Hello all, I'm working on a calculator program for complex numbers, and all my functions are working properly with the exception of my swap function.
The idea is too swap the value of the "answer" with the value of "A" but for some reason my "ans1,ans2,ans3, ans4" variables keep defaulting to zero resulting in the swap values being wrong despite having values obtained. Here's a snippet of the code showing the working division function and the broken swap function, any help would be appreciated.

ComplexNumber divi (const ComplexNumber& c) //division function
{
ComplexNumber comp;
comp.ans1= this->realA/c.realB;
comp.ans2= this->imagA /c.imagB;
comp.ans3= this->magnitudeA/c.magnitudeB;
comp.ans4= this->angleA /c.angleB;
return comp;
}

ComplexNumber ANS2A (const ComplexNumber& c) //Answer swapto A function

{


ComplexNumber comp;

comp.realA = this->realA + c.ans1;
comp.ans1 = this->realA - c.ans1;
comp.realA = this->realA-c.ans1;

comp.imagA = this->imagA + c.ans2;
comp.ans2 = this->imagA - c.ans2;
comp.imagA = this->imagA-c.ans2;

comp.magnitudeA = this->magnitudeA + c.ans3;
comp.ans3 = this->magnitudeA - c.ans3;
comp.magnitudeA = this->magnitudeA - c.ans3;

comp.angleA = this->angleA + c.ans4;
comp.ans4 = this->angleA - c.ans4;
comp.angleA = this->angleA - c.ans4;

return comp;
do you know about operator overloading?
it is a lot more mathy and prettier to overload division / :
consider

a = b/c;
vs
a = b.div(c);

and even better, imagine an overloaded = operator:

temp = a;
a = b;
b = temp; //hmm.. :)

I am not 100% sure what you want the end result to be from your function which not only swaps but is doing something else.
I strongly advise you isolate the swap from the other operation. But I suspect a design flaw -- it seems like you should NOT store the answers inside the class. That is going to blow up on you when you string multiple operations together, and it is going to be weird to track which variable holds the answers to various operations.


c++ has a complex type, if you didn't know, but writing your own is excellent practice.

if you can describe exactly what that function should do, I can try to help more regardless....
Last edited on
Topic archived. No new replies allowed.