Program crashing, need a little help!
Dec 19, 2012 at 9:16pm UTC
Hi everyone! Glad to be here.
I'd really appreciate if you guys could take a look at my little program, which I've done as an exercise to Accelerated C++, a really great book I'm going through right now.
The program just crashes and I have no idea why.
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout << "Enter a series of words, "
"followed by end-of-file: " ;
string current_word;
vector<string> word_list;
while (cin >> current_word) {
word_list.push_back(current_word);
}
sort(word_list.begin(), word_list.end());
typedef vector<string>::size_type vec_sz;
vec_sz size = word_list.size();
int count = 0;
string word_compare = word_list[0];
int current_index = 0;
do {
if (word_compare == word_list[current_index]) {
++count;
cout << "added count" << endl;
cout << current_index << endl;
}
else {
cout << "The word " << word_compare << " appears " << count << " times." << endl;
count = 0;
cout << "count reset" << endl;
}
++current_index;
word_compare = word_list[current_index];
} while (current_index < size);
return 0;
}
Thanks for the help!
Last edited on Dec 19, 2012 at 9:17pm UTC
Dec 19, 2012 at 9:22pm UTC
1 2 3
++current_index;
word_compare = word_list[current_index]; //out of bounds
} while (current_index < size);
Last edited on Dec 19, 2012 at 9:22pm UTC
Dec 19, 2012 at 9:31pm UTC
It seems so obvious now.
Thanks!
Topic archived. No new replies allowed.