Program to count each distinct word input
I am trying to solve this problem:
"Write a program to count how many times each distinct word appears in it's input" Accelerated C++ p49, exercise 3-3. So far, I have placed all the words input into a vector:
Code:
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
int main()
{
// program to count number of times each distinct word appears in input
// first - get all input from user
cout << "Please enter some words: (Ctl-D to end input) " << endl;
vector<string> words;
string word;
while(cin >> word)
{
words.push_back(word);
}
// test that some words have been entered
typedef vector<string>::size_type vec_sz;
vec_sz size = words.size();
if (size == 0)
{
cout << endl << "You must enter 1 word or more. Please try again. " << endl;
return 1;
}
// Now all words have been entered, the data processing can be done
}
What should I do next? I am thinking I should start with first element of vector and compare it to all other elements. If the string is repeated then add 1 to count for each repeated word. The problem is that my solution is not clear. I don't know if I need a separate vector to hold the count for each word or if count can just be an int? I just cant get anywhere with this and am worried as it is supposed to be a simple exercise that i should be able to do without help?