Word Counter

I need to make a program that counts the number of words in a string. I also need to count the number of times a word appears and then sort the array based on that number. I've got it to count the total words and how many times each one appears but it shows the word multiple times in the output instead of just once. Any ideas on how to do something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>

using namespace std;

struct Word {
    string word;
    int count;
};

class WordList {
    public: 
        int totalCount, distinctCount;
        Word words[200];       
};

int main() {
    WordList list;
    Word word2;
    //word2.count = 0;
    list.totalCount = 0;
    list.distinctCount = 0;
    bool done = false;
    int i = 0;
    string display[200];

    cout<<"Enter your sentences below. ### will terminate the input."<<endl;
    
    while(done == false) {
        cin>>list.words[i].word;
        list.totalCount++;
        
        if(list.words[i].word == "###") {
            list.totalCount--;
            done = true;
        }
        i++;
    }
    cout<<endl;
    
    for(int cnt = 0; cnt < list.totalCount; cnt++) {
        list.words[cnt].count = 0;
        for(int a = 0; a < list.totalCount; a++) {
            if(list.words[cnt].word == list.words[a].word) {
                list.words[cnt].count++;
                list.distinctCount++;
            }
            else {
                
                
            } 
        }
        cout<<list.words[cnt].word<<" "<<list.words[cnt].count<<endl;
        list.words[cnt].word.erase();
    }
    
    cout<<"Total Words: "<<list.totalCount<<endl;
    cout<<"Distinct Words: "<<list.distinctCount<<endl;
    
    system("pause");
    return 0;
}


Thanks in advance!
Based on a quick Google search (first result)
A working solution consists of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    string s("Somewhere down the road");
    istringstream iss(s);

    do
    {
        string sub;
        iss >> sub;
        cout << "Substring: " << sub << endl;
    } while (iss);

}


Source(s): http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
This helps a little bit but doesn't really answer my question. How would I get the output to look sort of like this?

"This is the string in the output. This string needs to be split up and counted."

Total Words: 16
Distinct Words: 13

this: 2
the: 2
string: 2
is: 1
in: 1
output: 1
needs: 1
to: 1
be: 1
split: 1
up: 1
and: 1
counted: 1

Topic archived. No new replies allowed.