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"); }
||and&&so it is not clear what condition you wrote there. use brackets. - Andrej Levkovitchifchecki + 3 < symbols.size()? - HolyBlackCat