// Write a program to count how many times each distinct word appears in its input.
#include <iostream>
#include <string>
#include <vector>
int main()
{
// Ask for
// and read the input words
std::cout << "Please input your words: "
<< std::endl;
std::vector<std::string> word_input;
std::string word;
int count = 0;
while (std::cin >> word)
{
word_input.push_back(word);
++count;
}
// Compare the input words
// and output the times of every word compared only with all the words
for (int i = 0; i != count; ++i)
{
int time = 0;
for (int j = 0; j != count; ++j)
{
if (word_input[i] == word_input[j])
++time;
else
break;
}
std::cout << "The time of "
<< word_input[i]
<< " is: "
<< time
<< std::endl;
}
return 0;
}
This is my codes for "Write a program to count how many times each distinct word appears in its input using C++". But I didn't realize the expected funtion. Will someone kind to point how to revise it? THANK YOU!
Sorry, I am new here, and not familiar with stack overflow. Now I will demonstrate in details.
If you compile and run this program, you will see:
Please input your words:
And I input as follows:
good good is good EOF
Then it shows:
The time of good is: 2
The time of good is: 2
The time of is is: 0
The time of good is: 2
But the result doesn't meet what I want.
My expected result is:
The time of good is: 3
The time of is is: 1
And now I think you probably have got what I meant to do.
I think the main problem is at this place:
for (int i = 0; i != count; ++i)
{
int time = 0;
for (int j = 0; j != count; ++j)
{
if (word_input[i] == word_input[j])
++time;
else
break;
}
I don't want to use MAP, because I haven't learned that so far. thank you~