Examining this:
while(!D.eof())
{
nr++;
if (nr == 1 || nr % 5 == 0)
{
D >> P.string[nr];
cout << P.string[nr] << " ";
}
D >> P.int_1[nr];
cout << P.int_1[nr] << " ";
}
The reason your nr
variable exceeds 5 is that you aren't resetting nr
when your after each successful read of each line. I don't know if that's what you want, but the way you've implemented it has a problem (see below):
Your structure element clearly has space for 10 elements, but you're only checking and storing the elements at indices 1 and those which are multiples of 5:
0, 1, 5, 10, etc.
Using the
eof
method is bad practice as @P0W has noted in his comment. Use a while loop instead in tandem withstd::getline
.