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
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
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.
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)
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/