hey, i was writing this program for complex number calc and when i run it ,it never returns me the right values..
so can u plz make necessary changes where i m wrong..
YEAH AND ONE IMP THING TO TELL U I M BEGINNING WITH POINTERS AND I LITERALLY SUCK AT IT!!!
so here's the code:

# include <cmath>
class Complex
{
private:
	double i, r;
public:
	int Set_C(double,double);
	int Get_I();
	int Get_R();
	int Add(Complex);
	int Sub(Complex);
	int Mul(Complex);
	int Div(Complex);
};
Complex::Set_C(double x,double y)
{
	r=x;
	i=y;
	return 0;
}
Complex::Get_R()
{
	return this->r;
}
Complex::Get_I()
{
	return this->i;
}
Complex::Add(Complex a)
{
	this->r=i+a.i;
	this->i=r+a.r;
	return 0;
}
Complex::Sub(Complex a)
{
	this->r=a.r-r;
	this->i=a.i-i;
	return 0;
}
Complex::Mul(Complex a)
{
	this->r=r*a.r-i*a.i;
	this->i=r*a.i+i*a.r;
	return 0;
}
Complex::Div(Complex a)
{
	this->r=((r*a.r)+(i*a.i))/(pow(r,2)+pow(i,2));
	this->i=((r*a.i)-(i*a.r))/(pow(r,2)+pow(i,2));
	return 0;
}

so this was the CLASS..

# include <iostream>
using namespace std;
void main ()
{
	Complex c1, c2;
	double x, y, s;
	char ch='y';
	
	while (ch=='y'||ch=='Y')
	{
	    cout<<"\nInput The Real And Imaginary Number For Complex Number C1:";
	    cin>>x>>y;
	    c1.Set_C(x,y);

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

	    cout<<"1.ADDITION OF TWO COMPLEX NUMBERS."<<"\n\n2.SUBTRACTION OF TWO COMPLEX NUMBERS."<<"\n\n3.MULTIPLICATION OF TWO COMPLEX NUMBERS."<<"\n\n4.DIVISON OF TWO COMPLEX NUMBERS."<<"\n\n5.FOR ALL.";
	    cout<<"\n\nInput The Option:";
	    cin >>s;


	    if(s==1 || s==5)
		{
		   cout<<"\nAddition Of Two Complex Numbers-------------------------------";
	       c2.Add(c1);
	       cout<<"\n"<<c2.Get_R()<<" + "<<c2.Get_I()<<"i\n";
		}

	    if(s==2 || s==5)
		{
		   cout<<"\nSubtraction Of Two Complex Numbers----------------------------";
		   c2.Sub(c1);
		   cout<<"\n"<<c2.Get_R()<<" + "<<c2.Get_I()<<"i\n";
		}

	    if(s==3 || s==5)
		{
		   cout<<"\nMultiplication Of Two Complex Numbers-------------------------";
		   c2.Mul(c1);
		   cout<<"\n"<<c2.Get_R()<<" + "<<c2.Get_I()<<"i\n";
		}

	    if(s==4 || s==5)
		{
		   cout<<"\nDivison Of Two Complex Numbers--------------------------------";
		   c2.Div(c1);
		   cout<<"\n"<<c2.Get_R()<<" + "<<c2.Get_I()<<"i\n";
		}
	    if(s > 5)
		{
	   	   cout<<"\nSELECTION OUT OF RANGE.";
		}
		cout<<"\nDo You Want To Restart The Calculation(Y for YES & N for NO):";
	    cin>>ch;
		system("cls");
	}
}

I get confused where to apply this-> pointer and where to not.

Is there a reason for returning 0 from your methods regardless of the outcome. Also in your definitions you need to put the return type

int Complex::Set_C(double x,double y) though I suspect you made them ints to make the compiler happy anyway which isn't the right approach. If you're not actually returning anything from your methods make them void.

Try and make some of those changes. I'm sure there's going to be stuff that still isn't working but see what happens.

EDIT: Also, hint: some of your methods will need to return an object of complex type.

Is there a reason for returning 0 from your methods regardless of the outcome. Also in your definitions you need to put the return type

int Complex::Set_C(double x,double y) though I suspect you made them ints to make the compiler happy anyway which isn't the right approach. If you're not actually returning anything from your methods make them void.

Try and make some of those changes. I'm sure there's going to stuff that still isn't working but see what happens.

but when i use void it shows 10 errors.
i modified it this way...

# include <cmath>
class Complex
{
private:
	double i, r;
public:
	void Set_C(double,double);
	int Get_I();
	int Get_R();
	void Add(Complex);
	void Sub(Complex);
	void Mul(Complex);
	void Div(Complex);
};
Complex::Set_C(double x,double y)
{
	r=x;
	i=y;
	
}
Complex::Get_R()
{
	return this->r;
}
Complex::Get_I()
{
	return this->i;
}
Complex::Add(Complex a)
{
	this->r=i+a.i;
	this->i=r+a.r;
	
}
Complex::Sub(Complex a)
{
	this->r=a.r-r;
	this->i=a.i-i;
	
}
Complex::Mul(Complex a)
{
	this->r=r*a.r-i*a.i;
	this->i=r*a.i+i*a.r;
	
}
Complex::Div(Complex a)
{
	this->r=((r*a.r)+(i*a.i))/(pow(r,2)+pow(i,2));
	this->i=((r*a.i)-(i*a.r))/(pow(r,2)+pow(i,2));
	
}

even when i use DOUBLE INSTEAD OF INT ,IT SHOWS ERRORS.

but when i use void it shows 10 errors.
i modified it this way...

# include <cmath>
class Complex
{
private:
	double i, r;
public:
	void Set_C(double,double);
	int Get_I();
	int Get_R();
	void Add(Complex);
	void Sub(Complex);
	void Mul(Complex);
	void Div(Complex);
};
Complex::Set_C(double x,double y)
{
	r=x;
	i=y;
	
}
Complex::Get_R()
{
	return this->r;
}
Complex::Get_I()
{
	return this->i;
}
Complex::Add(Complex a)
{
	this->r=i+a.i;
	this->i=r+a.r;
	
}
Complex::Sub(Complex a)
{
	this->r=a.r-r;
	this->i=a.i-i;
	
}
Complex::Mul(Complex a)
{
	this->r=r*a.r-i*a.i;
	this->i=r*a.i+i*a.r;
	
}
Complex::Div(Complex a)
{
	this->r=((r*a.r)+(i*a.i))/(pow(r,2)+pow(i,2));
	this->i=((r*a.i)-(i*a.r))/(pow(r,2)+pow(i,2));
	
}

even when i use DOUBLE INSTEAD OF INT ,IT SHOWS ERRORS.

though i have done it other way round by directly inserting the values in my declared variables in functions and it works perfect..that was my style...
but my professor uses the fuction call for calling the values.so i was trying to do it his way..

You're making a bigger deal out of the arrow operator than you need to.
When you have a class you access the (public) members via the dot operator. myClass.MyMethod() for example.
But when you have a pointer to a class you need to dereference it first via the * operator. So

ClassName * myClass = new ClassName();
*myClass gets you the class object back and
(*myClass).MyMethod() calls the method (note the parentheses which are necessary for doing the deref first and then the . operator)
The shorthand for 
(*myClass).MyMethod()
is
myClass->MyMethod()

So every class has a pointer to itself known as this. To get at one of your methods you need to dereference and dot, so this->MyMethod() Without the pointer you just use the dot but the pointer is what we have to work with.

Once you've modified this in your methods you need to return *this; thus what type is your return value??

Also, on lines 15,21,25,29,35,41,47 of post number 4 is where you need to specify the return value. These definitions are no different than when you have

int myRegularFunction (int parameter) 
{
      etc. etc.
}

You're making a bigger deal out of the arrow operator than you need to.
When you have a class you access the (public) members via the dot operator. myClass.MyMethod() for example.
But when you have a pointer to a class you need to dereference it first via the * operator. So

ClassName * myClass = new ClassName();
*myClass gets you the class object back and
(*myClass).MyMethod() calls the method (note the parentheses which are necessary for doing the deref first and then the . operator)
The shorthand for 
(*myClass).MyMethod()
is
myClass->MyMethod()

So every class has a pointer to itself known as this. To get at one of your methods you need to dereference and dot, so this->MyMethod() Without the pointer you just use the dot but the pointer is what we have to work with.

yup...done it...looks like first i have to tackle pointers before hazarding my programs with them.;)

So what datatype do add sub mul div return?

No. if you are returning *this what are you returning? What does this point to? An object of what type?

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.