hey!i was tryinn to write this complex number addition and i dunno where this garbage value it is pickin from :-/....
m gettin mad over this because first time such a simple program has got me all over :icon_mad:....
so here's the code:

# include <iostream>
using namespace std;
# include <cmath>
class Complex
{
private:
    double i, r;
public:
    Complex()
    {
        i=r=0;
    }
    void SetC(double x, double y)
    {
        r=x;
        i=y;
    }
    void A(Complex a,double x,double y)
    {
        Complex c;
        c.r=r+a.r;
        c.i=i+a.i;
        x=c.r;
        y=c.i;
    }
};

void main ()
{
    Complex c1, c2;
    double x, y, re=0, im=0;
    cout<<"\nInput The Real And Imaginary Number For Complex Number C1:";
    cin>>x>>y;
    c1.SetC(x,y);

    cout<<"\nInput The Real And Imaginary Number For Complex Number C2:";
    cin>>x>>y;
    c2.SetC(x,y);

    cout<<"\nThe Addition Of Two Complex Numbers Is As Follows:";
    c2.A(c1,re,im);
    cout<<re<<"+"<<im<<"i";
}

main should always return an int, not be void.
You need to make x and y reference variables, otherwise you're passing them in and they are changed but this is not reflected in the original variables. void A(Complex a,double & x,double & y)

commented: yup..the reference thing worked.;) +0

main should always return an int, not be void.

Why? It compiles to valid code, The extra return 0 is just more code to maintain.

SLEEPYBUG:
There are some general things that help when writing code
and it is best to start early:

give your functions meaningful names
A(); says little about your intention add() would be a better name.
Complex C;
is this intended just as a temporary variable fine
but it is not needed
x = a.r + r;
y = a.i + i;
will work with the references.
My concern is that you were intending to return C;
and have not done it. Did you intend this?

Complex Add(Complex a, double dr, double di);
Complex Add(Complex a, Complex b);

otherwise the reference will be fine but what is it you are wanting
the function to do ?
you might want a display() function for instance

Why? It compiles to valid code, The extra return 0 is just more code to maintain.

SLEEPYBUG:
There are some general things that help when writing code
and it is best to start early:

give your functions meaningful names
A(); says little about your intention add() would be a better name.
Complex C;
is this intended just as a temporary variable fine
but it is not needed
x = a.r + r;
y = a.i + i;
will work with the references.
My concern is that you were intending to return C;
and have not done it. Did you intend this?

Complex Add(Complex a, double dr, double di);
Complex Add(Complex a, Complex b);

otherwise the reference will be fine but what is it you are wanting
the function to do ?
you might want a display() function for instance

void A(Complex a,double x,double y)
	{
		Complex c;
		c.r=r+a.r;
		c.i=i+a.i;
		x=c.r;
		y=c.i;
	}

here i wanted to just put the added value in the suggested variable like above x and y from the main()..
when i tried to do it in the main() , i dunno but where it picked the garbage value..
becoz i think the function >>i checked it using debugging and it worked perfectly....can u just tell me from where it was picking the garbage value???

here i wanted to just put the added value in the suggested variable like above x and y from the main()..
when i tried to do it in the main() , i dunno but where it picked the garbage value..
becoz i think the function >>i checked it using debugging and it worked perfectly....can u just tell me from where it was picking the garbage value???

with i and r being private
does
a.r compile for A()
you need accessors or public varibales

I suspect that your output is if you compile
"0+0i"
ok there are two errors in your code both occurring at the same place when you call the A() function

your set function should be working fine.

1- if you look at what jonsca says it will fix your problem
when you put re and im into the function
the function creates two new variables
as if it says
double re2 = re;
double im2 = im;
and in your function you are changing re2 & im2

to change the value is as jonsca says you need a reference
look at the & he has added! try it

2 - this mistake won't stop you function from working but if you do
c = a + b;
or
a += b;
should really be how you perform an addition
so either update
your i & r
r += a.r;
i += i.ar;

then you need to get you private :i &r
add to your class

double get_imaginary(){return i;}
double get_real() {return

then in main

cout << c2.get_real() << '+' << c2.get_imaginary() << 'i' << endl;

or

Complex Add(Complex to_add_with_me)
{
Complex ret;
double ret_r = r + to_add_with_me.get_real();
double ret_i = i + to_add_with_me.get_imaginary();
ret.SetC(ret_r, ret_i);
return ret;
}
commented: precisely,just what i needed.thanks a lot man! +0

Very nice job with the examples. One thing though...

main should always return an int, not be void.

Why? It compiles to valid code, The extra return 0 is just more code to maintain.

Nope. It's non-standard.
Check out this article. Many compilers will not even compile with it these days. As far as your argument goes, even if you don't explicitly include the return 0; it is implied.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.