title: C++ how to validate input for use with overloaded >> operator
body: <p>I am writing a program that can do operations on complex numbers. I have a class called ComplexNumber that has the overloaded operators in it. My program takes input from a file in the form of <code>complex *operator* complex</code>. So, for example an input would look like <code>3+4i + 2+3i</code>. I have written my >> operator so this works fine. </p>
  
  <p>The issue arises when the input looks like <code>3i + 1+2i</code>. We have to validate the input so it works when the complex number is missing parts. It can be just a real number, or just an imaginary number.</p>
  
  <p>The functions in the ComplexNumber class that relate to this issue are as follows:</p>
  
  <pre><code>ComplexNumber::ComplexNumber(double r,double i)
  {
      realNum = r;
      imaginaryNum = i;
  }
  
  
  istream&amp; operator&gt;&gt;(istream &amp;input , ComplexNumber&amp; other)  //Overloaded &gt;&gt; operator
  {
      char filter = 0;
      double r =0;
      double i = 0;
  
      input &gt;&gt; r &gt;&gt; i &gt;&gt; filter;
      other.setR(r);
      other.setI(i);
  
      return input;
  }
  </code></pre>
  
  <p>And the way I am reading in the input in my main class is as follows:</p>
  
  <pre><code> void input(ifstream &amp;in)
  {
          ComplexNumber a,b;
          in &gt;&gt; a;
          in.get();
          string op;
          getline(in,op,' ');
          in &gt;&gt; b;
          cout &lt;&lt; a &lt;&lt; " " &lt;&lt; op &lt;&lt; " " &lt;&lt; b &lt;&lt; endl;
  }
  
  
  int main()
  {
      ifstream in("complex.txt");
      if(!in) cout &lt;&lt; "failed to open file." &lt;&lt; endl;
  
      while(!in.eof()){
          input(in);
      }
      return 0;
  }
  </code></pre>
  
  <p>For my operators to work, I need to set the missing part of the input as 0 in the object. So if the input was <code>3i</code> the variables in the object would be <code>realNum = 0, imaginaryNum = 3</code> How can I achieve this?</p>
  
  <p>How can I check the input on the line to decide how it should be read in? At the moment, it is expecting the complex number to have both a real and imaginary part to it.</p>
  
  <p>I also wrote an overloaded constructor for cases where the complex number only has one of the parts to it, but I am unsure how to use it. The function is as follows:</p>
  
  <pre><code>ComplexNumber::ComplexNumber(double in, string r_i)    //Overloaded constructor
  {
      if(r_i == "r"){realNum = in; imaginaryNum = 0;}
      else{imaginaryNum = in; realNum = 0;}
  }
  </code></pre>
  
  <p>Beyond this issue, we also have to check to make sure that the input has no invalid characters eg. <code>j or !</code> but i feel that if I get help with this first problem, I can use the information given to solve this second problem.</p>
  
  <p>I realize that this may not be worded in the best way, I just hope you understand what I am trying to achieve. I really appreciate any help with this. Thanks.</p>