The task of calculating the number of sentences. Several conditions have been created, the text is entered into the vector from the file. I assume that the error is that, for example, at the last iteration, when checking a vector with an index [i + 1] or [i + 2], the size of the vector will be exceeded, but if you comment out the 3rd and last condition, the error is not appears, although the penultimate condition includes checking the [i + 1] element. Tell me how to solve this problem and whether I think correctly.

#include <iostream> #include <fstream> #include <vector> using namespace std; int main() { int quant_sent = 0; //количСство ΠΏΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠΉ ifstream read; read.open("Text.txt"); vector<char> symbols; //символы ΠΈΠ· Ρ„Π°ΠΉΠ»Π° char n; while (read.get(n)) { symbols.push_back(n); } cout << symbols.size() << endl; for (int i = 0; i != symbols.size(); i++) // Π’ΠΎΡ‡Π½ΠΎ ΡΠ²Π»ΡΡŽΡ‚ΡΡ ΠΊΠΎΠ½Ρ†ΠΎΠΌ ΠΏΡ€Π΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠΉ { /* if ((int(symbols[i]) == 33) || (int(symbols[i]) == 63) || (int(symbols[i]) == 46) && (int(symbols[i + 1]) == 46) && \ (int(symbols[i + 2]) == 46) || (int(symbols[i] == 33) && int(symbols[i + 1] == 63)) || (int(symbols[i]) != 32) && \ (int(symbols[i + 2] == 46))) { quant_sent++; cout << quant_sent << " : " << i << endl; }*/ if (int(symbols[i]) == '!') { // ! quant_sent++; cout << quant_sent << " : " << i+1 << endl; } else if (int(symbols[i]) == '?') {// ? quant_sent++; cout << quant_sent << " : " << i + 1 << endl; } /*else if (int(symbols[i]) == '.') {// ... if (int(symbols[i + 1]) == '.') { if (int(symbols[i + 2]) == '.') { quant_sent++; cout << quant_sent << " : " << i + 1 << endl; // fix } } }*/ else if ((int(symbols[i]) == '!') && (int(symbols[i + 1]) == '?')) { // !? quant_sent++; cout << quant_sent << " : " << i + 1 << endl; } else if (((int(symbols[i]) != ' ') || (int(symbols[i]) != '.')) && (int(symbols[i + 2]) == '.') && (int(symbols[i+3]) != '.')) { quant_sent++; cout << quant_sent << " : " << i + 1 << endl; } } read.close(); cout << "The number of sentences " << quant_sent; system("pause"); } 
  • Describe what a sentence is in words. From the source it is not very clear ... And certainly there is no point in storing all the characters in a vector. - Harry
  • you are also mixed || and && so it is not clear what condition you wrote there. use brackets. - Andrej Levkovitch
  • one
    In general, the problem can be solved by adding fictitious spaces (or whatever you should have at the end of the sentence), checking only to the size of the vector. But this, solving the problem of going beyond the array, does not solve the problem of inefficiency ... - Harry
  • 2
    Can add to the very beginning of this if check i + 3 < symbols.size() ? - HolyBlackCat
  • Proposal (by convention): JD Allen has one dollar! I can find him and meet with him ... - Gabriel Mignola

1 answer 1

First, the completely unnecessary translation of char to int: int(symbols[i]) . Secondly, you have 3 and 4 conditions going beyond the array. Thirdly, condition 3 must be the first, since it will never be fulfilled. Well, and so on. If you want to sort through a vector, it's easier to do something like this:

  vector<char> symbols; //символы ΠΈΠ· Ρ„Π°ΠΉΠ»Π° char n; while (read.get(n)) { symbols.push_back(n); } cout << symbols.size() << endl; bool isLetter = true; char localBack; while(!symbols.empty()) { localBack = symbols.back(); if(localBack=='!' || localBack=='?' || localBack=='.') { if(isLetter) quant_sent++; isLetter = false; } else if (localBack!=' ') isLetter=true; symbols.pop_back(); } read.close(); cout << "The number of sentences " << quant_sent; system("pause"); 

True, I didn’t check it, but, it seems, it should work;) Without an exact TK, as you have already written, the task cannot be solved. For example:

Who did this? I did it!

Who did this? Ya. Ya. Ivanov did it. (Yakov Yakovlevich Ivanov)