Jaspreet's advice is correct; I'll add some background to it.
Your program logic is one valid way to read the data. (Another possibility, often more intuitive with line-oriented "records" of data, is to read indeed line by line and parse each line individually, which makes the end of each "record" obvious.)
Now why is ignore()
skipping lines? The reason is that the empty line has already been skipped at that point. In detail:
Your algorithm attempts to read numbers until that fails because the next word is no number (or because EOF was reached). Now the library logic for reading a number begins with skipping any leading whitespace, including newlines; you see the problem now. Only after the first letter of the next name is read does the number reading algorithm give up and puts the read letter back in the input. The skipped newlines are not put back. Then comes your ignore
and skips over the valid line that we are standing at the beginning of.
The good news is that with records whose internal structure indicates record boundaries (like here: records end with a sequence of numbers; the first non-number indicates the beginning of a new record) you can ignore any white space like line breaks and just parse word by word. This makes the program more robust: You can process data with no empty lines, or with multiple empty lines, or without any line breaks at all!
If the data could conceivably contain an occasional error though (say, a letter among the numbers) the line breaks can still be used as a spot at which re-synchronization could be attempted, in an effort of robust programming. But in your case re-synchronization would happen automatically (after reading one badly parsed "record" with a name with a number in it, probably).
As a final discussion fine point I would suggest to read from stdin and write to stdout with this type of data processing; leave the data source and destination to the caller (via myprog < infile.txt > outfile.txt
or such). Both the Windows and *nix command lines support this. That makes the program more versatile and saves programming work. If an assignment demands the two files to be read, separate the actual algorithmic work on the data (parse a record and compute the average) from obtaining and writing the data, which should be in a function that just gets an istream and ostream. That would make it possible to provide data from any source, for example strings, via stringstreams.
In fact, one could separate the parsing from the algorithmic work as well by defining a class which corresponds to a data record, and overload operator>>
for it, as well as a float record.averageScore()
member function :-). That would look more like C++.
Here are a few snippets of what might work. The playerT
is a class holding a "record" of data. I implemented the input function because it is somewhat tricky with clearing the input stream's fail bit.
/// Currently just a first name, family name and vector of scores
class playerT
{
string firstName;
string lastName;
vector<float> scores;
public:
float averageScore()
{
return scores.size()
? accumulate(scores.begin(), scores.end(), 0.0)/scores.size()
: 0; // avoid dividing by zero.
}
friend ostream & operator<<(ostream &os, const playerT &player);
friend istream &operator>>(istream &is, playerT &player);
};
/// Produces output which could be read back with
/// the input operator. In particular, does not
/// output the average.
ostream &operator<<(ostream &os, const playerT &player)
{
//...
}
// Error handling is left to the caller.
// In particular, after EOF the player must be tested for completeness.
istream &operator>>(istream &is, playerT &player)
{
is >> player.firstName >> player.lastName;
player.scores.clear();
float nextScore;
while(is >> nextScore)
{
player.scores.push_back(nextScore);
}
// Clear the expected format error indicating end of scores
// (and therefore this record),
// but not others which must be handled by the caller.
is.clear(is.rdstate() & ~ios::failbit);
return is;
}
The main function boils down to quite little, which makes the information flow clearer. The reason is that the dirty I/O details like clearing the fail bit etc. were moved to specialized functions, as well as the "business logic" of computing the average.
int main()
{
do // loop to eof
{
playerT newPlayer;
cin >> newPlayer;
// if this was the last record eof is now true
// and the loop will exit after printing the last record.
// todo: handle I/O errors
cout << newPlayer << " (Av.: " << newPlayer.averageScore() << ')' << endl;
}while(cin.good());
return cin.bad(); // eof is ok. only bad is bad.
}
getline
. You have a squirrely while loop. – AndyG Dec 30 '16 at 15:07ignore
is throwing away every second line. Comment that line out and it should write all the lines. – wally Dec 30 '16 at 15:09