i have a code for complex number but when i key in 7+i3 and 5+i2 into the operator+ it should return me the result of 12,5 but i receive 430. What is the problem wif my code?

class Complex
{
	friend istream& operator>>(istream&, Complex&);
	friend ostream& operator<<(ostream&, const Complex&);
public:
	Complex(int, int);		// constructors
	Complex operator +(Complex);
private:
	int a, b;	
};

Complex::Complex( int _a=0, int _b=0 ) 
{
   a = _a;
   b = _b;
}


istream& operator>>(istream &in, Complex &num)
{
    string line="";
    in>>line;
    for(int i=0; i<line.length(); i++)
    {
            if(i==1)
            {
                 num.a = line[i];
            }
            else if(i==4)
            {
                 num.b = line[i];
            }     
    }         
	return in;
}

ostream& operator<<(ostream &out, const Complex &num)
{
    out<<num.a;
    out<<num.b;
    return out;         
}

Complex Complex::operator +(Complex t)
{
    Complex temp;
    temp.a = a + t.b;
    return temp;       
}

int main()
{
    Complex A, B, C;
    cout << "Enter first complex number, format (a+ib): ";
    cin >> A;
    cout << "Enter second complex number, format (a+ib): ";
    cin >> B;

    C = A + B;
    cout << "Addition A + B = " << C << endl;
    system("pause");
    return 0;
}

Dani AI

Generated

Brief diagnosis (building on 's observations): there are multiple independent bugs here. The input-parsing loop uses hard-coded character positions and assigns characters directly into int fields (so you end up with ASCII codes like 43 for '+'). The operator+ implementation adds the wrong members and never sets the imaginary part. operator<< prints the two integers with no separator so values run together. Fix all of those and the result for 7+i3 + 5+i2 will be 12+i5.

Correct the arithmetic and formatting first:

Complex Complex::operator+(const Complex& t) const
{
    return Complex(a + t.a, b + t.b);
}

ostream& operator<<(ostream& out, const Complex& num)
{
    out << num.a << (num.b < 0 ? "-i" : "+i") << (num.b < 0 ? -num.b : num.b);
    return out;
}

Replace the brittle parsing loop with a simple, robust parser. A practical quick fix is to read the token and sscanf it into two ints; it handles signs and multi-digit numbers:

istream& operator>>(istream& in, Complex& num)
{
    string s;
    if(!(in >> s)) return in;
    int ra=0, rb=0;
    if(std::sscanf(s.c_str(), "%d+i%d", &ra, &rb) == 2) {
        num.a = ra; num.b = rb;
    } else {
        in.setstate(ios::failbit);
    }
    return in;
}

Extra tips: declare operator+ as const and take the right-hand operand by const&; initialize members in the constructor; handle failure by setting failbit; and prefer std::istringstream or regex if input formats may vary. With these fixes the class will parse 7+i3 and 5+i2 correctly and print 12+i5.

Recommended Answers

All 2 Replies

>out<<num.a;
>out<<num.b;
First I'd suggest you separate those two values with something. Your actual output is 43,0.

if(i==1)
{
  num.a = line[i];
}

At position 1 in "7+i3" is '+'. So the first problem is that you're assigning the wrong character. The second problem is that you're assigning the value of the character and not the value of the digit it represents. So even if you change that test to i == 0 , you'll still get (most likely) 55 instead of 7. For single digits you can subtract '0' from the digit to get the proper value:

if ( i == 0 )
{
  num.a = line[i] - '0';
}

Rinse and repeat.

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.