I need to count the number of character, lines and spaces in a file and the following code displays wrong results in the number of lines. Does anyone have any idea why and how can it be written in a better way?
What was the output and how does the input file look like.
One problem is the use of !input.eof() which doesn't work as you might expect - Google will tell you why. Normal way is while (input.get(character))
Another problem is that you read one character before the while loop - WHY ?
It's possible that the file doesn't end with '\n' so your line count is 1 short.
That said, if you subscribe to the Unix definition of a line, then characters after the last newline in the file are not part of a line. :3 This would make TC's algorithm technically correct... Based on TC's description it isn't clear that is what is meant by "incorrect" though.
Add a count to the number of lines for each loop.
Keep appending the length of the new line to get the number of characters.
Scan each line (range-based for loop) and add a count for each space.