Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several problems here. The first, and most important, is that most systems will do line oriented input, which means that they will pick up a lot of control characters (including, probably, ESC) and do something with them internally, rather than returning them to you; it might not even be possible to ever see an ESC in the input. </p> <p>For the rest, there are several problems with your code. The first is that you're testing against the index into the array (where you'll put the next character), not against anything you've read. The second is that you're inputting <code>int</code>, so you won't see any individual characters; just the results of the conversion of your input. (In fact, if an ESC character does get through, you'll end up with <code>std::cin</code> in a error state.) The third is that you never test whether the input has succeeded.</p> <p>A better solution would be to adopt some other convention for the input: say one number per line, with an empty line signaling the end. In that case, you could write something like:</p> <pre><code>std::vector&lt;int&gt; a; std::string line; std::cout &lt;&lt; "..." &lt;&lt; std::endl; while ( std::getline( std::cin, line ) &amp;&amp; !line.empty() ) { std::istringstream parse( line ); int value; parse &gt;&gt; value &gt;&gt; std::ws; if ( !parse ) { std::cout &lt;&lt; "That wasn't an integer, try again" &lt;&lt; std::endl; } else if ( parse.get() != EOF ) { std::cout &lt;&lt; "Extra garbage at end of line, ignoring it" &lt;&lt; std::endl; } else { a.push_back( value ); } } </code></pre> <p>(You'll note that I've used <code>std::vector</code>, since you don't know up front how many inputs you'll get.)</p> <p>If you really want to use ESC, you'll probably need a third party library, along the lines of <code>curses</code>, to do raw input, reading one character at a time, and collecting the digits into a string, for conversion using <code>std::istringstream</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload