ghosting final row of input, wt???

i am completely puzzled on this one. i have an *input* file that looks like this

michael jordan 23
larry bird 33
larry fitzgerald 11


i have been able to read this into three vectors, two string vectors for first / last name, and one INT vector for number

yet when i type in playernumber.size() the return value is 4 and not 3.

upon doing more research, the entire "4th row" of this input is a copy of the 3rd row, so in fact the vectors (placed side by side) look like

michael jordan 23
larry bird 33
larry fitzgerald 11
larry fitzgerald 11


has anyone ever had this problem? why is this happening?
http://www.cplusplus.com/forum/articles/1295/
You don't think it'd be useful to show us the code you've used to load it from the file?
tihs is an extension of a q from a prev thread so i didnt put it up, but here it goes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
  vector<string> player;
  vector<string> team;
  vector<int> number;


  ifstream infile( "bdata.txt" );
  string strplay;
  string strteam;
  int x;
  

  while(infile)
  {
               infile >>
     strplay >>
     strteam >>
     x;
     player.push_back(strplay);
     team.push_back(strteam);
     number.push_back(x);
     }
     
        infile.close();
     cout << "Total length: " << player.size() << endl;
}


in this case the input data was of the form *first name* *team* *number* and 3 rows, but the size was "4" and the entire 4th row a copy of the last row even though there was never a 4th row in the input
Last edited on
also, while on this subject, in that code i tried to initialize a new vector

1
2
3
vector<int> numcopy;

copy(number.begin(), number.end(), numcopy.begin() );


and i get a massive error, i can compile it without error but as soon as i run it the system closes and i get an error screen, purely because i have insterted this in there
Last edited on
As for your first issue. Trace through the steps of execution and you will see it's fairly simple.

Line 24 executes on the last one, and it goes ok, runs through the loop and Line22 is true, so it tries it again on the last line, and fails. But it still has to finish the loop. Thus you get 2 copies of your last entry in the vector (line 30).

As for your 2nd one. You need to resize the vector prior to copying into it.

numcopy.resize(number.size());
awesome, i haven't tried this yet big it sounds like the right answer to both, so big thanks :) , i am teaching myself this language out of school.


but i wish i could understand the reasoning behind the loop problem. your explanation sounds completely right, but i dont get it. the way i wrote this, i intended (or at least, wanted) the while loop to run as long as there is info to read. any time it sees info it sequentially extracts it into a vector.

so the moment it stops reading (after line 3 of the input) i am not seeing why line 22 doesnt equate to 'false' and the loop is skipped



also, in the case of the numcopy vector, i am never allowed to just declare it as an unsized empty vector and then copy into it? is there a reason why?

isnt that what i was able to do with the input in the first place? i am not understanding the limitation here :(
As for your loop. The loop condition is only every validate before each iteration. The infile is flagged as false half way through the loop. It will still complete the loop with the infile flagged as bad (thus creating the 2nd copy of your last line) before the loop condition is re-evaluated.

After line 27 you could add:
1
2
 if (!infile)
 break;


As for your vector. The function .push_back will allocate memory as required to store variables. The copy function assumes you've allocated the memory already (by using resize or having enough elements already in the vector)
i think it makes sense to me now, big thanks zaita

i like your idea for the break statement. i am curious if there is an alternative way i could express the while statement, i will be thinking of this
Yes.
1
2
3
while ( (infile >> x >> y >> z) ) {

}

should work.

Personally, I've always used getline(infile, mystring) and then split the string with mystring.substr() so I can better validate the input.
http://www.cplusplus.com/forum/articles/6046/

yep! it worked. i am reading through that thread again, i will research substr() too and maybe see if i can do it like that too.
Topic archived. No new replies allowed.