Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cin.eof() functionality

I understand that cin.eof() tests the stream format. And while giving input, end of character is not reached when there is wrong in the input. I tested this on my MSV C++ 2010 and am not understanding the strange results. No matter what I give the input, I am getting Format Error message that is present in the program.

#include <iostream>
using namespace std;

int main()
{
    int i;
    cin>> i;

    if(!cin.eof())
    {
        cout<< "\n Format Error \n";
    }
    else
    {
        cout<< "\n Correct Input \n";
    }

    getchar();
    return 0;
}

Results I expected:

Values for i =

  1. 10 => Correct Input but the output is Format Error
  2. 12a => Format Error

Could someone explain where I am going wrong. Thanks.

like image 599
Mahesh Avatar asked Feb 08 '11 22:02

Mahesh


People also ask

What is EOF value in C++?

EOF instead is a negative integer constant that indicates the end of a stream; often it's -1, but the standard doesn't say anything about its actual value. C & C++ differ in the type of NULL and '\0' : in C++ '\0' is a char , while in C it's an int ; this because in C all character literals are considered int s.

Why do we need EOF?

EOF is detected when there's no more data to read. When you write to a file, you don't need to write a special character; closing the file means that the file has a fixed size and EOF will be reported when reading and the read offset reaches the end of the file.

What does Cin get () do C++?

get() is used for accessing character array. It includes white space characters. Generally, cin with an extraction operator (>>) terminates when whitespace is found.


2 Answers

std::cin.eof() tests for end-of-file (hence eof), not for errors. For error checking use !std::cin.good(), the built-in conversion operator (if(std::cin)) or the boolean negation operator (if(!std::cin)).

like image 69
sbi Avatar answered Oct 13 '22 06:10

sbi


For an input stream to enter the EOF state you have to actually make an attempt to read past the end of stream. I.e. it is not enough to reach the end-of-stream location in the stream, it is necessary to actually try to read a character past the end. This attempt will result in EOF state being activated, which in turn will make cin.eof() return true.

However, in your case you are not only not doing that, you (most likely) are not even reaching the end of stream. If you input your 10 from the keyboard, you probably finished the input by pressing the [Enter] key. This resulted in a new-line character being added to the input stream. So, what you are actually parsing with >> operator in this case is actually a 10\n sequence. Since you requested an int value from the stream, it only reads the numerical characters from the stream, i.e. it reads 1 and 0, but it stops at \n. That \n remains in the stream. You never read it. So, obviously, your code never reaches the end-of-file position in the stream. You have to reason to expect cin.eof() to become true in such case.

like image 24
AnT Avatar answered Oct 13 '22 05:10

AnT