0

I am doing a simple code on identifying even and odd numbers in a text file using

Here is my code, the only error I supposedly get is the exclamation mark in the while loop

     #include <iostream>
     #include <fstream>
     using namespace std;

     int main()
    {
     int even = 0, odd = 0;
     int value;
     ifstream file3("evenodd.txt");

while (!file3.eof) {
    file3 >> value;
    if (value % 2 == 0) {
        even++;
    }
    else {
        odd++;
    }
    cout << " Even count: " << even << " ";
    cout << "Odd count: " << odd << " ";
 }
 }
Jos
  • 65
  • 7
  • 3
    `file3.eof` is the address of a member function. As such, the expression `!file3.eof` is invalid, and wouldn't actually check if the end of `file3` has been reached since it doesn't call the member function. You probably intend the loop to do `while (!file3.eof())`. Doing that is a bad idea though - read https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons for more information – Peter Apr 22 '20 at 20:05
  • Thank you for the answer, noted. – Jos Apr 22 '20 at 20:18

1 Answers1

1

eof is a function so the code need to look like this while (!file3.eof()).

Remo
  • 69
  • 2