Why is this printing out twice?

Ok so for some reason this outputs the vector twice. Not sure why

1
2
3
4
5
6
7
while(nameFile.good() == true)
	{
		getline(nameFile, temp);
		names.push_back(temp);
		std::cout << names[index] << std::endl;
		index++;
	}
Maybe...

1
2
3
4
5
6
7
8
while(nameFile.good() == true)
	{
		getline(nameFile, temp);
                if(!nameFile.good()) break;
		names.push_back(temp);
		std::cout << names[index] << std::endl;
		index++;
	}
Wouldn't it stop looping once it hit eof though?

EDIT:
That did work. Now my question is why doesnt it stop when it hits eof?
Last edited on
while( ((((((nameFile.good() == true) == true) == true) == true) == true) == true) == true ) just to be sure.
Use while( getline(nameFile, temp) ) instead.

¿Twice like in aabbccddee or abcdeabcde?
Last edited on
I guess that second while would be good lol. I do like your first implementation though :D
Prints twice as in abc abc
nameFile.txt

1
2
3
4
vector
vector
vector
EOF


1) nameFile is opened
2) nameFile status is good
2) Enter loop first time.
3) Read first vector // note that read operation can change nameFile status
4) nameFile status is still good
5) Enter loop second time
6) Read vector
7) nameFile status is still good
8) Enter loop third time
9) Read vector
a) nameFile status is still good
b) Enter loop fourth time //oh oh
c) Read vector
d) EOF read so nameFile status is now bad but still in loop




Topic archived. No new replies allowed.