I'm using cin to read in words that are then sorted into a vector along with a counter for how many times they have been entered, but the last word always seem to be read in twice.
For exampe, if I enter "foo bar", then it will say:
foo 1
bar 2
instead of:
foo 1
bar 1
I can't find anything wrong with the insert function, so I'm guessing that something is wrong with how the input is received.
Code:string input; while (!cin.eof()) { cin >> input; insert(word_list, input); }Code:void insert (vector<word_entry> &word_list, string input) { // Convert input to lowercase transform(input.begin(), input.end(), input.begin(), ::tolower); vector<word_entry>::iterator i; for (i = word_list.begin(); i <= word_list.end(); i++) { word_entry elem = {input, 1}; if (i == word_list.end()) { word_list.push_back(elem); break; } if (i->word.compare(input) > 0) { word_list.insert(i, elem); break; } else if (i->word.compare(input) == 0) { i->count++; break; } } }



LinkBack URL
About LinkBacks


