Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>operator>> will stop reading at the first space (as David pointed out) - use getline instead</p> <pre><code>std::string a; getline(std::cin, a); </code></pre> <p>Full edit of your code</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;limits&gt; int main() { std::cout &lt;&lt; "%%%%%%%%%%%%%%%%%%String Reversing App%%%%%%%%%%%%%%%%%%%%%%%%\n\n"; std::cout &lt;&lt; "\nEnter 1 to continue and 0 to exit" &lt;&lt; std::endl; int inputa; std::cin &gt;&gt; inputa; if(std::cin &amp;&amp; inputa!=0) { std::cin.ignore(std::numeric_limits&lt;int&gt;::max( ), '\n'); do { std::string a,c; std::cout&lt;&lt;"\nEnter the string you want to Reverse : "; getline(std::cin, a); for(int x=a.length()-1; x&gt;=0; --x) { c+=a[x]; } std::cout&lt;&lt;"\nThe Reverse String is : " &lt;&lt; c &lt;&lt; std::endl; std::cout &lt;&lt; "\nEnter 1 to continue and 0 to exit" &lt;&lt; std::endl &lt;&lt; std::endl; std::cin &gt;&gt; inputa; std::cin.ignore(std::numeric_limits&lt;int&gt;::max( ), '\n'); } while(std::cin &amp;&amp; inputa!=0); } } </code></pre> <p>Including David's verbatim answer because he answered with much more detail (David Rodríguez - dribeas) - please +1 him before he deletes it. His answer adds much more information that I did not mention so we are merging this into a single reply at Davids request,</p> <p>The answer by Adrian is correct, deals with the immediate issue and provides a solution. As to why it enters an infinite loop, the reason is that after reading the first word, you are trying to read an integer <code>std::cin &gt;&gt; inputa</code>, which will fail as <code>cde</code> cannot be parsed as an integer. At this point the stream enters a fail state and subsequent reads will fail without doing anything (until you clear the error state).</p> <p>What should you do?</p> <p>If you want to process whole lines, then you should use <code>std::getline</code>, rather than <code>operator&gt;&gt;</code>. Beware on mixing both, as operator>> won't consume the spaces after the read (including new lines) and you might just read an empty line with the next <code>std::getline</code>. You can either always read with <code>std::getline</code> and then parse the line, or use <code>ignore</code> to clear up to the newline. Finally, whenever you perform IO operations, don't expect the operation to succeed: check the state of the stream. If you don't and your loop depends on IO to complete, it is quite easy to enter this sort of infinite loop, where the stream is marked as failed, no later reads succeed and you never break out of the loop.</p>
    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.
    3. 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