title: How to print complex numbers in C++
body: <p>I am playing around with classes in C++. Currently I am working on a class for complex numbers and want to be able to print them in the following format: <code>-2+3i</code>, <code>1-4i</code>. That means, that I want the real part to have only a sign if it is negative. In contrast the imaginary part should have always a sign whether it is positive or negative.</p>
  
  <p>I tried the following which did not work as expected:</p>
  
  <pre><code>inline void Complex::print() const {
      std::cout &lt;&lt; x;
      std::cout &lt;&lt; std::showpos &lt;&lt; y &lt;&lt; "i" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>This method prints also for the real part the sign if it is positive. Why does <code>std::showpos</code> affect the first line?</p>
  
  <p>Is there any better way to do that?</p>