1
#include <iostream>
using namespace std;

int main()
{
    int x;
    cin >> x;
    if (x == EOF)
        cout << x;
    system("pause");
}

Inputting the EOF on Windows output nothing. While -1 outputs -1.

Over here

#include <iostream>
using namespace std;

int main()
{
    int x;
    if ((x=cin.get()) == EOF)
        cout << x;
    system("pause");
}

Inputting the EOF on Windows outputs -1. While -1 outputs nothing.

Now I am totally confused (I am working on 64-bit Windows 7 with Visual Studio 2015; though I do not think this is related)

I also want to add if "x" is assigned EOF in both cases, from where the difference came? I am comparing the value of "x" to EOF in both cases, right?

hussamash
  • 33
  • 6
  • 1
    `cin.get()` returns `EOF` if there's no data, so of course that comparison passes. EOF is not a character like you're treating it. If you input EOF, it's not a character read and stored as `EOF`. `EOF` is just a magic number representing a state. – chris Aug 07 '15 at 01:53
  • 1
    @chris I dont understand you to be honest :/ If EOF is not caharacter what is assigned to "x" in the first case ? And what in the second case? Because I am comparing "x" to EOF in both cases right? – hussamash Aug 07 '15 at 01:58
  • The [input fails](http://en.cppreference.com/w/cpp/locale/num_get/get) in the first case because there's nothing to read (entering EOF ends the data, it isn't data itself) and convert to `int` to store into `x`. However, storing `EOF` into the result wouldn't even work in all cases due to types like pointers that can't store it. You have to check `std::cin`'s flags there. The input [still fails](http://en.cppreference.com/w/cpp/io/basic_istream/get) in the second case, but it makes use of the return value to return `EOF`. – chris Aug 07 '15 at 02:07
  • 1
    `x` isn't assigned to `EOF` when you use `cin >> x`. `istream::operator>>` doesn't do that when it hits `EOF`. `istream::get` does. They're different functions; they work differently. – DrPizza Aug 07 '15 at 02:08
  • @DrPizza Thanks now I get it (Y). – hussamash Aug 07 '15 at 02:14
  • @chrisThanks now I get it (Y). – hussamash Aug 07 '15 at 02:14

1 Answers1

0

EOF is a macro that expands to a negative int (usually -1).

This is a number returned by some input functions to indicate that an end-of-file occurred. It is nothing to do with the way that the end-of-file condition was triggered by your operating system (be it pressing ^Z, or running out of input, or whatever).

The code:

int x;
cin >> x

performs formatted input. The >> and << operators are for formatted I/O. It means to read a textual representation of a number and convert that to int. The only way you will get x == -1 is if you actually type in -1, as you have found.

To detect whether end-of-file occurred you can inspect the stream via cin.eof() after doing the read. (Note that normally you should check for all failure modes, not just end-of-file).

This code:

if ((x=cin.get()) == EOF)

will match if end-of-file occurred, because istream::get() is one of those few functions that indicates an end-of-file condition via the value returned. Again however, you are outputting the value of the EOF macro, which is unrelated to what you did in your operating system to generate the end-of-file condition.

Community
  • 1
  • 1
M.M
  • 130,300
  • 18
  • 171
  • 314