Computing word-frequency-table I got a word count assignment and the requirements are shown below:


  INPUT
  
  The input contains words in several lines and in each line
  there could be more than one word. The input only contains English
  words, separated by space or line breaks. Words are case-sensitive.
  
  OUTPUT
  
  Print the words in alphabetic order with the corresponding appearance
  count in the text. For each word, please print the result in a single line
  (separate the word and the number with space).


Sample Input:

Computer system
computer design
algorithm design and analysis
quantum computer
computer science Department


Sample Output:

Computer 1
algorithm 1
analysis 1
and 1
computer 3
department 1
design 2
quantum 1
science 1
system 1


I tried something shown below and actually got the same output as the sample but still got marked as wrong answer by the assignment judge system.
Is there anything I did wrong?

#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;iterator&gt;
#include &lt;vector&gt;
using namespace std;

int main() {

    string whole;
    string line;
    string word;

    do {
        getline(cin, line);
        whole += line + ' ';
    } while (!line.empty());

    if (!whole.empty()) {
        istringstream iss(whole);
        vector&lt;string&gt; results((istream_iterator&lt;string&gt;(iss)), istream_iterator&lt;string&gt;());

        int count = 1;
        int size = results.size();
        if (size == 1) cout &lt;&lt; results[0] &lt;&lt; ' ' &lt;&lt; count;
        else {
            sort(results.begin(), results.end());
            for (int j = 0; j &lt; size; j++) {
                if (j != size - 1) {
                    if (results[j] != results[j + 1]) {
                        string word = results[j];
                        cout &lt;&lt; word &lt;&lt; ' ' &lt;&lt; count &lt;&lt; endl;
                        count = 1;
                    }
                    else if (results[j] == results[j + 1]) count++;
                }
                else if (j == size - 1) {
                    if (results[j] != results[j - 1]) {
                        string word = results[j];
                        cout &lt;&lt; word &lt;&lt; ' ' &lt;&lt; count &lt;&lt; endl;
                    }
                    else if (results[j] == results[j - 1]) cout &lt;&lt; results[j] &lt;&lt; ' ' &lt;&lt; count &lt;&lt; endl;
                }
            }
        }
    }
    return 0;
}
@Deduplicator said almost everything, I will only add that it's really important that you know algorithms and data structures from the STL and using the proper in each case.
Here, you do so much work for nothing:

#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;string&gt;
#include &lt;map&gt;

int main() {
    auto in = std::istream_iterator&lt;std::string&gt;{std::cin};
    auto out = std::map&lt;std::string, int&gt;{};

    std::for_each(in, {}, [&amp;out](const std::string&amp; w) { out[w]++; });
    for (auto&amp; w : out) { std::cout &lt;&lt; w.first &lt;&lt; ' ' &lt;&lt; w.second &lt;&lt; '\n'; }
}