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;
}