Home c++ Vector subscript out of range in a for loop

Vector subscript out of range in a for loop

Author

Date

Category

The task of calculating the number of sentences. Several conditions have been created, the text is entered into the vector from the file.
I suppose that the error is that, for example, at the last iteration, when checking the vector with the index [i + 1] or [i + 2], there will be an overshoot of the vector size, but if you comment out the third 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 if I am thinking correctly.

#include & lt; iostream & gt;
#include & lt; fstream & gt;
#include & lt; vector & gt;
using namespace std;
int main ()
{
  int quant_sent = 0; // number of offers
  ifstream read;
  read.open ("Text.txt");
    vector & lt; char & gt; symbols; // symbols from file
    char n;
    while (read.get (n))
    {
      symbols.push_back (n);
    }
    cout & lt; & lt; symbols.size () & lt; & lt; endl;
    for (int i = 0; i! = symbols.size (); i ++) // Exactly end of sentences
    {
      / *
      if ((int (symbols [i]) == 33) || (int (symbols [i]) == 63) || (int (symbols [i]) == 46) & amp; & amp; (int (symbols [i + 1]) == 46) & amp; & amp; \
        (int (symbols [i + 2]) == 46) || (int (symbols [i] == 33) & amp; & amp; int (symbols [i + 1] == 63)) || (int (symbols [i])! = 32) & amp; & amp; \
        (int (symbols [i + 2] == 46)))
      {
        quant_sent ++;
        cout & lt; & lt; quant_sent & lt; & lt; ":" & lt; & lt; i & lt; & lt; endl;
      } * /
      if (int (symbols [i]) == '!') {//!
        quant_sent ++;
        cout & lt; & lt; quant_sent & lt; & lt; ":" & lt; & lt; i + 1 & lt; & lt; endl;
      }
      else if (int (symbols [i]) == '?') {//?
        quant_sent ++;
cout & lt; & lt; quant_sent & lt; & lt; ":" & lt; & lt; i + 1 & lt; & lt; endl;
      }
      / * else if (int (symbols [i]) == '.') {// ...
        if (int (symbols [i + 1]) == '.') {
          if (int (symbols [i + 2]) == '.') {
            quant_sent ++;
            cout & lt; & lt; quant_sent & lt; & lt; ":" & lt; & lt; i + 1 & lt; & lt; endl; // fix
          }
        }
      } * /
      else if ((int (symbols [i]) == '!') & amp; & amp; (int (symbols [i + 1]) == '?')) {//!?
        quant_sent ++;
        cout & lt; & lt; quant_sent & lt; & lt; ":" & lt; & lt; i + 1 & lt; & lt; endl;
      }
      else if (((int (symbols [i])! = '') || (int (symbols [i])! = '.')) & amp; & amp; (int (symbols [i + 2]) == '.') & amp; & amp; (int (symbols [i + 3])! = '.')) {
        quant_sent ++;
        cout & lt; & lt; quant_sent & lt; & lt; ":" & lt; & lt; i + 1 & lt; & lt; endl;
      }
    }
    read.close ();
      cout & lt; & lt; "The number of sentences" & lt; & lt; quant_sent;
      system ("pause");
}

Answer 1, authority 100%

First, the completely unnecessary conversion of char to int: int (symbols [i]) .
Secondly, you have out of bounds of the array in conditions 3 and 4.
Thirdly, condition 3 should be the first, since it will never be fulfilled for you. Well, etc.
If you want to iterate over a vector, then it’s easier to do something like this:

vector & lt; char & gt; symbols; // symbols from file
  char n;
  while (read.get (n))
  {
    symbols.push_back (n);
  }
  cout & lt; & lt; symbols.size () & lt; & lt; 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 & lt; & lt; "The number of sentences" & lt; & lt; quant_sent;
  system ("pause");

True, I haven’t tested it, but it seems to work;)
Without an exact TK, as you have already been written, the problem cannot be solved. For example:

Who did it? Me. I did it!

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

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions