I am working on a simple C++ program that reads files and counts the number of words in the file. I've arrived at code that produces the same output. Which code is "correct", if any?
I had understood before editing this question that infile >> str1 is considered a statement being used as an expression. Is that true or not true and is it considered "correct" to use the first block of code.
The first method uses:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream infile;
string str1;
int count;
count = 0;
while (infile >> str1) {
count++;
}
infile.close();
cout << count << endl;
}
The second method uses:
count = 0;
infile >> str1;
while (infile) {
count++;
infile >> str1;
}