Hello ladies and gents,
I'm trying to do another exercise from Accelerated C++ in wich I have to write a program to count how many times each distinct word appears in its input.
What I have untill now is this,
int main()
{
vector<string> theWords;
string aWord;
short count;
short amount = 0;
while(cin >> aWord)
theWords.push_back(aWord);
count = theWords.size();
cout << "Amount of words is " << count << endl;
for (int i = 0; i < count; ++i)
for (int j = 0; j <= i; ++j)
if (theWords[i] != theWords[j])
{
theWords[i] = theWords[j];
++amount;
}
cout << "Different amount of words is " << amount;
for (i = 0; i < amount; ++i)
cout << theWords[i] << "\n";
cout << "\n";
std::cout << "Press enter to exit.\n";
std::cin.ignore(2, '\0');
return 0;
}
I'm getting the correct amount of different words, but, I'm having a problem in getting them displayed, I only get the first word in the list.
I know it has to do with this theWords = theWords[j];
but don't know how to get it right?
Could someone help me out. Thanks