Sunday, 8 September 2013

Getting unexpected large integer

Getting unexpected large integer

I'm following an example to count how many times a word occurs in given
input. Here is my code:
string word, the_word;
int count(0);
vector<string> sentence;
auto it = sentence.begin();
cout << "Enter some words. Ctrl+z to end." << endl;
while (cin >> word)
sentence.push_back(word);
the_word = *sentence.begin();
cout << the_word << endl;
while(it != sentence.end()) {
if(*sentence.begin() == the_word)
++count;
++it;
}
cout << count << endl;
The input I'm giving is "how now now now brown cow cow". I expect count to
be 3, but instead I get an integer in the 2 millions. What am I missing?

No comments:

Post a Comment