file into vector (last line being copied twice)

Hey guys, Im trying to copy lines of a txt file into a vector. Everything is working fine except the last line is being copied twice. I cant figure out why

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main (){
	vector<Person> persident(20);
	int i=0;
	string fname, lname;
	int month, day, year;
	string text;
	
ifstream myfile("presidents.txt");
if (myfile.is_open())
{
while ( getline (myfile, text))
{

istringstream ss (text);
ss >> fname >> lname >> month >> day >> year;

persident[i].set_name(fname, lname);
persident[i].set_birday_day(month, day, year);
	
i++;
}
}
else cout << "Unable to open file" << endl;
Add a check for end of file after your call to getline().
If your file has some blank lines after the last valid entry you'll see this type of problem. To insure you properly parsed the string you should check the stringstream state before adding the data to your vector.
Topic archived. No new replies allowed.