Why is this printing out twice?

Jan 29, 2012 at 3:16am
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++;
	}
Jan 29, 2012 at 4:11am
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++;
	}
Jan 29, 2012 at 4:14am
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 Jan 29, 2012 at 4:16am
Jan 29, 2012 at 4:20am
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 Jan 29, 2012 at 4:21am
Jan 29, 2012 at 4:27am
I guess that second while would be good lol. I do like your first implementation though :D
Prints twice as in abc abc
Jan 29, 2012 at 4:35am
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.