How can i get a complex number

Hi.

I want to get some complex numbers and use them as points in plane.

This is my code but I cant get the complex numbers from input.Please help me :D

#include <complex>
#include <iostream>

using namespace std;

typedef complex<double> point;

int main () {
point a;
cin>>a.real()>>a.imag(); // I DONT KNOW HOW I CAN WRITE THIS LINE !
cout<<real(a)<<" "<<imag(a)<<endl;
return 0;
}
Last edited on
I've never used complex, but if its a standard class, then the >> operator should already be overloaded.

(ie) cin>>a should be sufficient

And I think it should be:
cout<<a.real()<<" "<<a.imag();
It does though from the looks of it, it will require the user to type a specific format which includes parens.

Alternatively, complex<double> has the following constructor:
complex( const double& r = double(), const double& i = double() )

I would use that constructor.

Ask the user to enter two doubles - first the real part then the imaginary. Then use the above constructor with the two values entered as parameters to construct the double.

Topic archived. No new replies allowed.