-2

In file called abc.txt, I have input following text:

sample text
sample text
sample text
sample text
sample text

Firstly I created variable(named text) for saving text- read from the file. Then program reads file abc.txt. I created vector named: ArrSent for saving each line from the file abc.txt. After loop while ends program close the file abc.txt. Then program have to output all sentences from vector ArrSent to the screnn.I have this kind of problem: after end of program, appears alert with message: vector subscript out of range. I have no idea why..

#include<iostream>
#include<string>
#include<fstream>
#include<vector>

using namespace std;

void function()
{
    string text;//variable to save text from file
    ifstream myfile("abc.txt");//reading from file colled abc.txt




    vector<string> ArrSent;

    if (myfile.is_open())
    {
        //cout <<"myplik.good()= "<< myfile.good() << endl;

        while (myfile.good())
        {   
                getline(myfile, text);
                ArrSent.push_back(text);
        }


        myfile.close();
    }
    for (int i = 0; i <= ArrSent.size(); i++)
    {
        cout << ArrSent[i] << endl;
    }

}


int main()
{
    function();
    system("pause");
    return 0;
}
user2534454
  • 355
  • 3
  • 12
  • 1
    `is_open()` and `good()` are completely pointless, and the `push_back` after your `getline` is flat out wrong, since you ignore the return value. – Kerrek SB Oct 08 '14 at 21:18
  • 1
    Please get rid of the [system("pause")](http://www.gidnetwork.com/b-61.html) -- it's dangerous and non-portable. – David Schwartz Oct 08 '14 at 21:22

1 Answers1

4

It is wrong here

for (int i = 0; i <= ArrSent.size(); i++)
{
    cout << ArrSent[i] << endl;
}

should be

for (int i = 0; i < ArrSent.size(); i++)
{
    cout << ArrSent[i] << endl;
}

The reason for that is that in C/C++, vector/array are zero based. That is to say that, if you have a vector, my_vector, size of 10, it goes like, my_vector[0], my_vector[1], ... my_vector[9]. There is no my_vector[10].

A better way to iterate through it , could be (C++11)

for (const auto & v : ArrSent)
{
    cout << v << endl;
}

or

for (vector<string>::const_iterator i = ArrSent.begin(); i != ArrSent.end(); ++i)
    cout << *i << endl;

As pointed out by WhozCraig, the while loop for reading is also buggy, a better version could be

  while (getline(myfile, text))
  {   
        ArrSent.push_back(text);
  }

A Word About function

Notable: Your function name is function. While that may be descriptive, you should know that standard library headers can freely include other standard library header (and very frequently do just that). One such header in the standard library is <functional> which declares, as luck would have it, std::function.

Why would you care? Because your using namespace std; brings everything in std out in the open with no namespace qualifier requirements, including potentially std::function (whether or not you included <functional>).

Which means although this will still compile:

void function()
{
     // .. stuff
}

This may not:

int main()
{
    function(); // HERE
    //... other stuff
}

This doesn't know whether you're calling your function or attempting to instantiate a single temporary of type std::function<> (which it can't anyway, as no template parameters are described). The result may be a ambiguous.

You can fix this by using ::function(); in your main(), but it would be better if you got developed the habit of not slurping in the entire std library in via using namespace std;, and/or not using names of common types/ids from the standard library.

WhozCraig
  • 59,130
  • 9
  • 69
  • 128
CS Pei
  • 10,211
  • 1
  • 23
  • 41
  • 1
    And in fact, this should be using either iterators or ranged-for enumeration in the first place. – WhozCraig Oct 08 '14 at 21:17
  • 1
    May as well fix the broken assumption they `getline` worked while your at it. The read-loop as-written is only slightly better than a `while(!std::cin.eof())`, which is [nearly always wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – WhozCraig Oct 08 '14 at 21:19