complex number to power

My program performs operations on complex numbers, however, I'm confused on how my exponent function can take only one complex number and take it to a power. I keep on getting an errror.
header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef complex_1_hpp
#define complex_1_hpp

#include<iostream>
#include <stdio.h>
using namespace std;

class complex
{
public:
    void InputC(istream &in);
    void OutputC(ostream &out);
    complex AddC(complex x);
    complex SubC(complex x);
    complex MultC(complex x);
    complex DivC(complex x);
    complex ExpC(complex x, int n);// this function
private:
    float real, imag;
};

#endif /* complex_1_hpp */


driver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include "complex_1.hpp"

complex complex::AddC(complex x)
{
    complex temp;
    temp.real= real +x.real;
    temp.imag= imag + x.imag;
    return(temp);
}

complex complex::SubC(complex x)
{
    complex temp;
    temp.real= real -x.real;
    temp.imag= imag - x.imag;
    return(temp);
}

complex complex::MultC(complex x)
{
    complex temp;
    temp.real= (real * x.real)-(imag * x.imag);
    temp.imag= (imag * x.real)+(real * x.imag);
    return(temp);
}
complex complex::DivC(complex x)
{
    complex temp;
    temp.real = (real*x.real + imag*x.imag)/(x.real*x.real+x.imag*x.imag);
    temp.imag = (imag*x.real - real*x.imag)/(x.real*x.real + x.imag*x.imag);
    return(temp);
}
 complex complex:: ExpC ( complex x, int n)
 {
        for (int i=1; i<n; i++)
            x = MultC(x);
    return x;
 }
void complex:: InputC(istream &in)
{
    char i,paren,comma;
    in >> paren>> real>> comma>> imag >>i>> paren;
}

void complex:: OutputC(ostream &out)
{
    cout << '(' << real << ',' << imag << "i)" << endl;
}

implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include "complex_1.hpp"
int main()
{
    char choice;
    complex a,b,c;
    int n;
    cout << endl;
    cout << "Enter a complex number pair such as (5.1,2.3i): ";
    a.InputC(cin);
    cout << "Enter a complex number pair such as (5.1,2.3i): ";
    b.InputC(cin);
    cout << "Enter a choice: (a)dd, (s)ub,(m)ult,(d)ivide, or (e)xponent: ";
    cin >> choice;
    switch (choice)
    {
        case 'a': c= a.AddC(b);
            
            break;
        case 's': c= a.SubC(b);
            
            break;
        case 'm': c= a.MultC(b);
            
            break;
        case 'd': c= a.DivC(b);
            
            break;
        case 'e' :
        cout << "Enter an integer for the compelx number to be taken a power to " << endl;
        cin >> n;
        c=a.ExpC(b,n); 

            break;
        default : cout << "Entry error";
    }
    cout << endl;
    cout << "Result is: ";
    c.OutputC(cout);
    cout << endl;
    return 0;
}
MultC is a member function of a class, not a standalone function.

Did you mean
x = x.MultC( x, n);
on line 37 of what you have (mistakenly) called driver?

Also, the way you have set it up is very odd. Surely you would want to write
c=a.ExpC(n);
and not what you have on line 32 of what you (mistakenly) call implementation? Object a is effectively redundant there.
Last edited on
I keep on getting an errror.

What is the error? Post it here, please.

If this is not a school assignment:

C++ has a complex number class and function templates for manipulating complex numbers in the <complex> header, including a function for raising to power: std::pow().

http://www.cplusplus.com/reference/complex/

http://www.cplusplus.com/reference/complex/pow/
Topic archived. No new replies allowed.