0
votes

I have here a code that is supposed to ask the user two sets of real and imaginary numbers.

#include <iostream>

using namespace std;

class Complex {
    public:
        double r;
        double i;
    public:
        Complex();
        void add(Complex, Complex);
        void subtract(Complex, Complex);
        void print();
};



Complex::Complex() {
    r = i = 0;
}

void Complex::add (Complex op1, Complex op2) {
    r = op1.r+op2.r;
    i = op1.i+op2.i;
}

void Complex::subtract (Complex op1, Complex op2) {
     r = op1.r-op2.r;
     i = op1.i-op2.i;
}

void Complex::print () {
    cout << r << i;
}

int main () {
    Complex operand1, operand2, result;
    cout << "Input real part for operand one: " << endl;
    cin >> operand1.r;
    cout << "Input imaginary part for operand one: " << endl;
    cin >> operand1.i;
    cout << "Input real part for operand two: " << endl;
    cin >> operand2.r;
    cout << "Input imaginary part for operand two: " << endl;
    cin >> operand2.i;
    result.add(operand1, operand2);
    cout << "The sum is " << result.add << endl;
    result.subtract(operand1, operand2);
    cout << "The difference is " << result.subtract << endl;
}

However, when I compiled the program, lots of errors are displayed (std::basic_ostream) which I don't even get.

Another issue I'm having is in the function void::Complex print. There should be a condition inside cout itself. No if-else. But I have no idea what to do.
The program must run like this:
Input real part for operand one: 5
Input imaginary part for operand one: 2 (the i for imaginary shouldn't be written)
Input real part for operand two: 8
Input imaginary part for operand two: 1 (again, i shouldn't be entered)
/then it will print the input(ed) numbers/
(5, 2i) //this time with an i
(8, 1i)
/then the answers/
The sum is 13+3i.
The difference is -3, 1i. //or -3, i

Please help me! I'm new in C++ and here in stackoverflow and your help would be very appreciated. Thank you very much!

4
Is this your school homework?penartur
Read some more about operator overloading and you should be able to write your add and subtract functions properly.Alexander
Yes, penartur. I think I did what I can but my knowledge is still lacking. I need guidance.user1156075
Which compiler are you using? g++ can be quite cryptic. Maybe try clang++? If not, google individual errors. Put some spirit into it :Dslezica
Hello, upside down! I use CodeBlocks. Thank you!user1156075

4 Answers

2
votes

The line

cout << "The sum is " << result.add << endl;

is incorrect, as add is a method so result.add will be a pointer to that method, and cout does not know how to handle it - which makes the compiler spit it out.

Change the line to

cout << "The sum is ";
result.print();
cout << endl;

You need to do the same for the line

cout << "The difference is " << result.subtract << endl;

As to coding style, the two methods are overwrting an existing complex number. Perhaps having a the function like this would be better

Complex &Complex::add (const Complex &op) { 
    r += op.r; 
    i += op.i;
    return *this;
}

This will enable you to chain additions together and also just add a complex number to the existing complex number.

In addition you could make the class variables r and i private. This will require an alternative constructor:

Complex:Complex(double real, double imaginary) : r(real), i(imaginary) {};

Finally you may wish to consider operator overloading - I am sure you can google that to find a reasonable tutorial.

0
votes

In main, after you call result.add, you put the same function in the cout stream when it doesn't return anything. I think you meant to write cout << "the sum is " << result.print();

0
votes

Try again with slight corrections

    #include <iostream.h>
    class Complex {
        public:
        double r; //real part
        double i; //imaginary part
        public:
        void add(Complex, Complex);
        void subtract(Complex, Complex);
        void print();
    };

    void Complex::add (Complex op1, Complex op2) {
        r = op1.r + op2.r;
        i = op1.i + op2.i;
    }

    void Complex::subtract (Complex op1, Complex op2) {
         r = op1.r - op2.r;
         i = op1.i - op2.i;
    }

    void Complex::print () {
        cout << "("<<r<<", " << i <<")";
    }

    void main () {
        Complex operand1, operand2, result;
        cout << "\nInput real part for operand one: " << endl;
        cin >> operand1.r;
        cout << "Input imaginary part for operand one: " << endl;
        cin >> operand1.i;
        cout << "Input real part for operand two: " << endl;
        cin >> operand2.r;
        cout << "Input imaginary part for operand two: " << endl;
        cin >> operand2.i;
        cout << "\nThe sum is ";
        result.add(operand1, operand2);
        result.print();
        cout << "\nThe difference is ";
        result.subtract(operand1, operand2);
        result.print();
    }
0
votes

You are already using the std:: namespace. Just use the complex number library in it like this answer suggests: Addition of complex numbers using classes