I am trying to count the number of words in a file, I know this question has been asked but I have tried some implementations I have seen but I keep getting an error.
The line in the file I am reading is "Super Chill" but when I run the code I get a count 3, where >> gets Super the first time and then Chill twice. I have a couple questions regarding this method:
1) what does While(in) look for? How does it know when to stop?
2) Why is "Chill" getting stored twice with >>?
Here is the code
int countWords(std::istream& in){ // line in file is -> Super Chill
int count = 0;
std::string word;
while (in) {
in >> word;
if (word != "") {
count+= 1;
}
}
return count;
}