Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ program not moving past cin step for string input
    primarykey
    data
    text
    <p>I'm obviously not quite getting the 'end-of-file' concept with C++ as the below program just isn't getting past the "while (cin >> x)" step. Whenever I run it from the command line it just sits there mocking me.</p> <p>Searching through SO and other places gives a lot of mention to hitting ctrl-z then hitting enter to put through an end-of-file character on windows, but that doesn't seem to be working for me. That makes me assume my problem is elsewhere. Maybe defining x as a string is my mistake? Any suggestions about where I'm going wrong here would be great.</p> <p>Note: sorry for the lack of comments in the code - the program itself is supposed to take in a series of words and then spit back out the count for each word.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; #include &lt;iomanip&gt; using std::cin; using std::cout; using std::endl; using std::sort; using std::string; using std::vector; int main() { cout &lt;&lt; "Enter a series of words separated by spaces, " "followed by end-of-file: "; vector&lt;string&gt; wordList; string x; while (cin &gt;&gt; x) wordList.push_back(x); typedef vector&lt;string&gt;::size_type vec_sz; vec_sz size = wordList.size(); if (size == 0) { cout &lt;&lt; endl &lt;&lt; "This list appears empty. " "Please try again." &lt;&lt; endl; return 1; } sort(wordList.begin(), wordList.end()); cout &lt;&lt; "Your word count is as follows:" &lt;&lt; endl; int wordCount = 1; for (int i = 0; i != size; i++) { if (wordList[i] == wordList[i+1]) { wordCount++; } else { cout &lt;&lt; wordList[i] &lt;&lt; " " &lt;&lt; wordCount &lt;&lt; endl; wordCount = 1; } } return 0; } </code></pre>
    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.
 

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