-1

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;
}
  • 3
    You don't know what statements are. – melpomene Jan 22 '18 at 00:11
  • 1
    Overloaded shift operator returns the stream reference itself, so your 2 *expression* have no difference. No *statement* here. – llllllllll Jan 22 '18 at 00:18
  • `while ( condition ) statement` from: http://en.cppreference.com/w/cpp/language/while You may also like to read: http://en.cppreference.com/w/cpp/language/statements – Richard Critten Jan 22 '18 at 00:22
  • `infile` is a variable, not a statement. – user207421 Jan 22 '18 at 00:35
  • As stated in my first paragraph the code works the same, and I would like to know if using one method is better vs the other method. – user2913891 Jan 22 '18 at 00:36
  • No, you are asking, incorrectly, about expressions *versus* statements. It's in your title and your question. – user207421 Jan 22 '18 at 00:41
  • As stated in my first paragraph the code produces the same output, and I would like to know if using one method is better vs the other method. According to the documentation linked by @Richard Critten a condition is an expression. But if `infile >> str1` where to be stated in the body of the while loop, it would be a statement, correct? So I am using a statement as a condition. The second block of code evaluates the condition infile as a Boolean expression. – user2913891 Jan 22 '18 at 00:49
  • 1
    Re your edit: sigh. Both methods use the *variable* `infile` as an *expression* in the *condition*; `infile >> str1` is an *expression*; your title is still meaningless; and [tag:expression] and [tag:statement] have nothing to do with your title or your question and should not appear anywhere, neither title nor question nor tags. The actual difference between these two pieces of code is zero except that the second is more verbose, which I never prefer. It also takes more space. – user207421 Jan 22 '18 at 00:54
  • Thanks - what am I to do? Delete the question and figure out how to ask a better one by doing what... not asking questions? Sorry I'm trying to learn something here. – user2913891 Jan 22 '18 at 01:00
  • Edit your title, question, and tags so it all makes sense. – user207421 Jan 22 '18 at 01:01
  • Possible duplicate of [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) –  Jan 22 '18 at 03:30

2 Answers2

1

The second example uses the same statement twice. That kind of repetition is a red flag. The first, using the extractor in the controlling expression for the loop, doesn’t have the repetition and looks much more idiomatic.

Pete Becker
  • 69,019
  • 6
  • 64
  • 147
0

infile >> str1 is considered a statement being used as an expression.

In the second case. Not in the first case.

is it considered "correct" to use the first block of code.

Both are correct, as they both compile and both execute the same. The second is both more verbose and larger in code size. It also contains a repeated statement, which is generally considered poor practice from the point of view of maintenance.

user207421
  • 289,834
  • 37
  • 266
  • 440