Hi again, hope I posted this correctly. I wrote some code to simply count words, lines and characters in a text file. But when i run this entire code the output is incorrect. If i only run the word counter i get the right answer; this also applies for lines. My character counter gives me a wrong output every time. How do I run this code without one counter messing up the other?
To count the lines you read the entire file. ¿In what part do you instruct the program to go back to the beginning to now count the words/characters?
Or instead of reading the file three times, you may process each line.
Also, don't loop on eof, loop on the reading operation while( the_file >> word )
Ok, now i understand to go back to the beginning of the program.
The extra to the while statement is in case a file is empty. so it will return 0 0 0 because it was returning 0 1 1. but im still getting the character/letter counter wrong. can u see why? im getting 1790 when it should be 1791.
ps
thanks for the earlier comment. It helped me so much.
> The extra to the while statement is in case a file is empty. so it will
> return 0 0 0 because it was returning 0 1 1.
No, that's wrong.
Suppose that the file has only one character, an space ' '.
1 2 3 4 5 6
while(true) {
if(the_file.peek() == EOF) //it passes, there's an space
break;
the_file >> word; //this would execute (and fail)
w++; //you've counted a word.
}
Again, loop on the input operation.
However, for the characters it should be the same. Perhaps you see different in windows if the file ends with an end-of-line or not.
You may do while( the_file >> std::noskipws >> c ) instead.