0

Hey i wrote this program but im getting some type of error in one of the lines of my code. This program is suppose to add, subtract, and print the complex numbers. I wont type out the whole assignment but this is basically what it does. I'll really appreciate it if someone can help me with this program. My code is at the bottom

#include <iostream>
#include <iomanip>
using namespace std;

// class declaration
class Complex {
  public:
    Complex( double = 0.0, double = 0.0 ); // default constructor
    Complex addition( const Complex & );
    Complex subtraction( const Complex & );
    void printComplex( void );
    void setComplexNumber( double, double );
  private:
    double realPart;
    double imaginaryPart;
};

// member functions definition
Complex::Complex( double real, double imaginary )
   { setComplexNumber( real, imaginary ); }

Complex Complex::addition( const Complex &a )
{
    Complex t;

    t.realPart = realPart + a.realPart;
    t.imaginaryPart = imaginaryPart + a.imaginaryPart;

    return t;
}

Complex Complex::subtraction( const Complex &s )
{
    Complex t(realPart - a.realPart, imaginaryPart - a.imaginaryPart);

    return t;
}

void Complex::printComplex( void )
   { cout << '(' << realPart << ", " << imaginaryPart << ')'; }

void Complex::setComplexNumber( double rp, double ip )
{
    realPart = rp;
    imaginaryPart = ip;
}

//driver program
int main()
{
    Complex b( 1, 7 ), c( 9, 2 );
    Complex a;

    b.printComplex();
    cout << " + ";
    c.printComplex();
    cout << " = ";
    a = b.addition(c);
    a.printComplex();
    cout << endl;

    b.setComplexNumber( 10, 1 );   // reset realPart and imaginaryPart
    c.setComplexNumber( 11, 5 );

    b.printComplex();
    cout << " - ";
    c.printComplex();
    cout << " = ";
    a = b.subtraction(c);
    a.printComplex();
    cout << endl;

    return 0;
}

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Ptolemy
0

>This program is suppose to add, subtract, and print the complex numbers.
It'd be 100% easier if you used the standard complex stuff from the <complex> header. Despite the name, it's actually quite easy. ;)

>but im getting some type of error in one of the lines of my code.
Is the error something like (Error: "a" is an unrecognized identifier)? And the line number takes you to this member function?

Complex Complex::subtraction( const Complex &s )
{
    Complex t(realPart - a.realPart, imaginaryPart - a.imaginaryPart);
    
    return t;
}

If so, you probably meant to use s instead of a. Beware the evils of copy/paste programming. :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.