title: C++ istream input fails yet consumes data. Expected to be able read bad data after failure
body: <p>My understanding of iostream has always been that when an input conversion fails, the data remains in the stream to be read after clearing the error.  But I have an example that does not always work that way, and I would like to know if the behavior is correct.  If so, could someone point me at some good documentation of the actual rules?</p>
  <p>This small program represents the idea of using an i/o failure to read an optional repeat count and a string.</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  int main()
  {
      int cnt;
      std::cout &lt;&lt; &quot;in: &quot;;
      std::cin &gt;&gt; cnt;
      if(!std::cin) {
          cnt = 1;
          std::cin.clear();
      }
      std::string s;
      std::cin &gt;&gt; s;
      std::cout &lt;&lt; &quot;out: &quot; &lt;&lt; cnt &lt;&lt; &quot; [&quot; &lt;&lt; s &lt;&lt; &quot;]&quot; &lt;&lt; std::endl;
  }
  </code></pre>
  <p>So, here's how it runs:</p>
  <pre><code>[me@localhost tmp]$ ./bother 
  in: 16 boxes
  out: 16 [boxes]
  [me@localhost tmp]$ ./bother 
  in: hatrack
  out: 1 [hatrack]
  [me@localhost tmp]$ ./bother 
  in: some things
  out: 1 [some]
  [me@localhost tmp]$ ./bother 
  in: 23miles
  out: 23 [miles]
  [me@localhost tmp]$ ./bother 
  in: @(#&amp;$(@#&amp;$ computer
  out: 1 [@(#&amp;$(@#&amp;$]
  </code></pre>
  <p>So it mostly works.  When there's a number first, it is read, then the string is.  When I give a non-numeric first, the read fails, the count is set to 1 and the non-numeric input is read.  But this breaks:</p>
  <pre><code>[me@localhost tmp]$ ./bother 
  in: + smith
  out: 1 [smith]
  </code></pre>
  <p>The + fails the integer read because it's not enough to make a number, but the + does not remain on the stream to be picked up by the string read.  Likewise with -.  If it reads the + or - as a zero, that would be reasonable, but then the read should succeed and cnt should show that zero.</p>
  <p>Perhaps this is correct behavior, and I've just always been wrong about what it is supposed to do.  If so, what <em>are</em> the rules?</p>
  <p>Your advice appreciated.</p>