Extraction operator >>

I am working on how to extract the real and imaginary component of a complex number (example 67 - 23i). My professor gave us a clue and said used the extraction operator>>.

I look on the website and never was surprise to see that they have these many choices

//MEMBER FUNCTION
 ``1234567891011121314151617181920212223`` ``````istream& operator>> (bool& val ); istream& operator>> (short& val ); istream& operator>> (unsigned short& val ); istream& operator>> (int& val ); istream& operator>> (unsigned int& val ); istream& operator>> (long& val ); istream& operator>> (unsigned long& val ); istream& operator>> (float& val ); istream& operator>> (double& val ); istream& operator>> (long double& val ); istream& operator>> (void*& val ); istream& operator>> (streambuf* sb ); istream& operator>> (istream& ( *pf )(istream&)); istream& operator>> (ios& ( *pf )(ios&)); istream& operator>> (ios_base& ( *pf )(ios_base&)); istream& operator>> (istream& is, char& ch ); istream& operator>> (istream& is, signed char& ch ); istream& operator>> (istream& is, unsigned char& ch ); //GLOBAL FUNCTION istream& operator>> (istream& is, char* str ); istream& operator>> (istream& is, signed char* str ); istream& operator>> (istream& is, unsigned char* str );``````

My question is:
1. Can use one of these functions, enter the complex number and immediately get the REAL and IMAGINARY part or do I write a code to parse my complex number myself to find the REAL amd IMAGINARY part?
I'm not too exPerinced in this, but I beleive that your imaginary number would be treated as an int. then you would need to parse the array using the '-' sign as the divider. The best thing to use is std::ifstream.getline()
Last edited on
There are many more than what you've listed, and there are other ways to parse strings besides the various overloads of operator>>, but to follow your requirements, it could be something like:

 ``12345678910111213141516`` ``````#include #include #include int main() { std::istringstream buf("67 - 23i"); char minus, i; double re, im; buf >> re >> minus >> im >> i; if(!buf || minus != '-' || i != 'i') std::cout << "Parse error\n"; else std::cout << std::complex(re, im) << '\n'; }``````

demo: http://ideone.com/l084xV
 I ... was surprise to see that they have these many choices

And there are even more! Including, from

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

 ``123456`` ``````template basic_istream& operator>> (basic_istream& istr, complex& rhs); template basic_ostream& operator<< (basic_ostream& ostr, const complex& rhs);``````

but it uses a different format:

 ``12345678910111213141516171819`` ``````#include #include using namespace std; int main() { const char input[] = " ( 3 , -7 ) "; cout << "input = " << input << "\n\n"; std::istringstream iis(input); std::complex c; iis >> c; cout << "c: real = " << c.real() << ", imag = " << c.imag() << "\n\n"; cout << "output = " << c << "\n\n"; return 0; }``````

 ```input = ( 3 , -7 ) c: real = 3, imag = -7 output = (3,-7)```

Last edited on
Topic archived. No new replies allowed.