title: Operator>> Overloading
body: <p>I am implementing a complex number using operator overloading. In the program, the user enters a complex number ALWAYS in the form:</p>
  
  <pre><code>a + bi
  </code></pre>
  
  <p>So, in example...</p>
  
  <pre><code>25.0 + 3.6i
  </code></pre>
  
  <p>Assume that the user will always enter both the real and the imaginary parts of the complex number, e.g., the user will enter "5 + 0i" (and not "5") or "0 - 6.2i" (and not "-6.2i").</p>
  
  <p>My problem is that in main() I have the following code:</p>
  
  <pre><code>ComplexNumber c1;
  cin &gt;&gt; c1;
  cout &lt;&lt; c1;
  </code></pre>
  
  <p>and the code prints: </p>
  
  <pre><code>0 + 0i
  </code></pre>
  
  <p>...when I entered "4.2 + 8.3i" into the prompt during runtime.</p>
  
  <p>Here is my implementation of my <code>operator&gt;&gt;</code> class:</p>
  
  <pre><code>istream &amp; operator&gt;&gt;(istream &amp; in, ComplexNumber &amp; n) {
      string real;
      string imag;
      bool done = false;
      int sign = 1;
  
      string num;
      in &gt;&gt; num;
  
      int length;
      for (int i = 0; i &lt; num.length(); i++) {
          if (num.at(i) == 'i') {
              imag = num.substr((i - length), i);
          }
          else if (num.at(i) == '-') {
              sign = -1;
          }
          else if (num.at(i) == ' ') {
              if (!done) {
                  real = num.substr(i);
                  done = true;
              }
              length = 0;
          }
          length++;
      }
  
      n = ComplexNumber(atof(real.c_str()), atof(imag.c_str()) * sign);
      return in;
  }
  </code></pre>
  
  <p>Here is my implementation of <code>operator&lt;&lt;</code> class:</p>
  
  <pre><code>ostream &amp; operator&lt;&lt;(ostream &amp; out, const ComplexNumber &amp; n) {
      n.print(out);
      return out;
  }
  </code></pre>
  
  <p>Here is my implementation of the ComplexNumber member class print():</p>
  
  <pre><code>void ComplexNumber::print(ostream &amp; out) const {
      if (imag &gt;= 0)
          out &lt;&lt; real &lt;&lt; " + " &lt;&lt; imag &lt;&lt; "i";
      else 
          out &lt;&lt; real &lt;&lt; " - " &lt;&lt; (-1 * imag) &lt;&lt; "i";
  }
  </code></pre>
  
  <p>This is my ComplexNumber header file for further details:</p>
  
  <pre><code>#ifndef COMPLEXNUMBER_H
  #define COMPLEXNUMBER_H
  
  #include &lt;iostream&gt;
  using namespace std;
  
  class ComplexNumber {
  
    public:
  
      // constructors
      ComplexNumber();
      ComplexNumber(double real_part, double imaginary_part);
      ComplexNumber(const ComplexNumber &amp; rhs);
  
      // named member functions
      void print(ostream &amp; out = cout) const;
      bool equals(const ComplexNumber &amp; rhs) const;
  
      // assignment operators
      const ComplexNumber &amp; operator=(const ComplexNumber &amp; rhs);
      const ComplexNumber &amp; operator+=(const ComplexNumber &amp; rhs);
      const ComplexNumber &amp; operator-=(const ComplexNumber &amp; rhs);
      const ComplexNumber &amp; operator*=(const ComplexNumber &amp; rhs);
  
    private:
      double real;      
      double imag; 
  };
  
  // arithmetic operators
  ComplexNumber operator+(const ComplexNumber &amp; lhs, const ComplexNumber &amp; rhs);
  ComplexNumber operator-(const ComplexNumber &amp; lhs, const ComplexNumber &amp; rhs);
  ComplexNumber operator*(const ComplexNumber &amp; lhs, const ComplexNumber &amp; rhs);
  
  // relational operators
  bool operator==(const ComplexNumber &amp; lhs, const ComplexNumber &amp; rhs);
  bool operator!=(const ComplexNumber &amp; lhs, const ComplexNumber &amp; rhs);
  
  // I/O operators
  ostream &amp; operator&lt;&lt;(ostream &amp; out, const ComplexNumber &amp; n);
  istream &amp; operator&gt;&gt;(istream &amp; in, ComplexNumber &amp; n);
  
  #endif
  </code></pre>
  
  <p>Any help with my implementations would be great.</p>