Program Fails to run

i'm trying to get my code to read from a text file, then find the first and last words of each sentence. I can get it to recognize Capitalized words, and the end of the sentences but when i try to thrown in a case to where the capitalized word follows the end of a sentence or is the first word of the text, it errors out with expression vector subscript out of range. Here's my code. It's a bit sloppy right now, but i'm just trying to get it to work first so I can transform it into a fileparser class and a linked list class

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
vector<string> sentences;
string current_sentence;

static void filter(const string& word, const string& word2){
	if(!word.empty() && ::isupper(word[0])){
		if(word2.back() == '.' || word2.empty())
	cout << word << "  ";
	}
	if(word.back() == '.'|| word.back() == '?' ||word.back() == '!' || word.back() == '--') 
			 cout<< "'" << word << endl;

}

int main(int argc, char** argv)
{
		 ifstream file;
		 file.open("story.txt");	
		 if(!file.good()){
			 return 2;
		 }
		 while(getline(file, current_sentence)){
			 istringstream ss(current_sentence);
			 string word;
			 while(ss >> word)
				 sentences.push_back( word );
		 
			 for(size_t i = 0; i < sentences.size(); i++){
		filter(sentences[i], sentences[i-1]);
				 

		 }
		 }

		 system("pause");
		 return 0;
	
		 
}


Any help would be greatly appreciated
Maybe the capitalised thing is a red herring.

You could define the start of the sentence to be the first word or the first word after a word that ends with .

That way you could detect sentences as you read from the file stream and store them directly.

Once you have a collection of sentences, you can do what you like.
Topic archived. No new replies allowed.