テキスト ファイルからベクターに文字列値を追加しようとしています。その際、文字列の文字数もチェックし、目的の文字数の単語に到達したら、その文字列をベクトルに追加します。
しかし、プログラムをデバッグしていると、範囲外の例外が表示され続け、必要な文字数よりも多い文字列が出力されます。
vector<string> words;
vector<string> chosenWords;
ifstream in("WordDatabase.txt");
while(in) {
string word;
in >> word;
cout << word << endl;
//push in selected words based on num of chars
words.push_back(word);
}
for(vector<string>::iterator itr=words.begin(); itr!=words.end();++itr)
{
if((*itr).length() >= 2 || (*itr).length() <= 7)
{
cout << (*itr) << endl;
chosenWords.push_back(*itr);
}
}