Because data from file look like this: line 1 is name (first last), next line is score (score1 score 2 ....score5) and so on... So I think that I need getline for name and >> for score
Example of data file
David Beckham
80 90 100 20 50
Ronaldinho Gaucho
99 80 100 20 60
....
First of all, I have structure
struct Player {
string name;
int score[5];
} player[size]
When read data from file
int i = 0;
while(!file.eof())
{
for (int j = 0; j < 2; j++) //read each 2 two lines
{
if(j==0) // name
{
getline(file, player[i].name);
}
else if(j==1) // score
{
for(int k=0; k<5; k++) file >> player[i].grade[k];
}
}
i++; //move to next player
}
Problem is after read all scores (of first player), it seems like doesn't go to next line to continue read next name, kind of mess up there. So any suggestions to correct my code or new idea to do this?
After reading the last score, the line break is still sitting on the input buffer. You need to skip that. The
ignore
function is useful for that.Check for errors as appropriate. The
>>
operator,getline
, andignore
all return the stream reference, which you can check for success or failure.There's no need for that
j
loop since each iteration does a completely different thing. Just write thej=0
case immediately followed by thej=1
case, and then get rid of the loop, like my code above. (And note thatj
will never equal 2 inside the loop, so your condition was wrong anyway.)Your main problem is that you are reading the integers with >> directly from the stream. This combined with reading a string from the stream is a bad idea. Reading the strings removes the new line will reading with >> will not remove the new lines.
It is best not to mix the two forms. Either always use >> or always use getline(). Note: I am not saying best I am saying easiest. You can use them together when you understand the tradeoffs and how to compensate for the differences in their usage.
Thus it is easier to read the line of numbers into a string then parse the string.
It is nearly always wrong to use:
This is because the EOF flag is not set until you read past the eof. Note the last read will read upto but not past the eof. Thus you will enter the loop even though there is not data available.
The standard pattern is:
With this in mind I would define a class that represents all the information you want (ie two lines). A simple version would be
This is fine if all you want to do is read lines.
But the data looks like it has a structure. So I would construct a class that represents the data and then read the data directly into that structure. So this is more what I would do. I would also replace the while() loops with algorithms.
Headers
The Class:
Usage: