Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As your program is right now it will read the long long's until cin's operator void* indicates that the value read was not compatible (but still prints the n , filled with garbage,that it failed to read), as you notice it ignores the blank lines. </p> <p>If I understand correctly, what you want is that when the user inputs a blank line the program should output the numbers read, a simple way to do this would be to read the input as a string, then check the string's length, and use a stringstream to convert it to the appropriate type.</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; void recursive() { std::string input; long long n; if ( std::getline(std::cin,input) &amp;&amp; input.length() &gt; 0 ) { std::istringstream ss(input); if (ss &gt;&gt; n) { recursive(); std::cout &lt;&lt; n &lt;&lt; std::endl; } } } int main () { recursive(); return 0; } </code></pre> <p><strong>edit</strong></p> <p>Here's another way, if your input comes in the form <code>46 798 4163 4654 32132</code>, it uses std::cin.peek() that returns the next character in the input without extracting it, the result of peek() is compared to '\n' to test if the user pressed enter, and if that's the case it prints the number it just read without waiting for the next number</p> <pre><code>#include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt; void recursive_in_a_line () { long long n; if (std::cin &gt;&gt; n) { if (std::cin.peek() != '\n') { recursive(); } std::cout&lt;&lt; n &lt;&lt; std::endl; } } int main () { recursive_in_a_line(); return 0; } </code></pre>
    singulars
    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. 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