Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if (cin >> x) - Why can you use that condition?

People also ask

What does cin >> n mean in C++?

cin is the standard input stream. Usually the stuff someone types in with a keyboard. We can extract values of this stream, using the >> operator. So cin >> n; reads an integer.

What type does Cin return?

cin is an object of class istream that represents the standard input stream. It corresponds to the cstdio stream stdin . The operator >> overload for streams return a reference to the same stream. The stream itself can be evaluated in a boolean condition to true or false through a conversion operator.

Does CIN overwrite?

Every time you read from cin to a variable, the old contents of that variable is overwritten.

How do you use the cin function?

The usage of cin is simple, too, and as cin is used for input, it is accompanied by the variable you want to be storing the input data in: std::cin >> Variable; Thus, cin is followed by the extraction operator >> (extracts data from the input stream), which is followed by the variable where the data needs to be stored.


cin is an object of class istream that represents the standard input stream. It corresponds to the cstdio stream stdin. The operator >>overload for streams return a reference to the same stream. The stream itself can be evaluated in a boolean condition to true or false through a conversion operator.

cin provides formatted stream extraction. The operation cin >> x;

where "x" is an int will fail if a non-numeric value is entered. So:

if(cin>>x)

will return false if you enter a letter rather than a digit.

This website on tips and tricks using C++ I/O will help you too.


Note: Answer updated four years after the fact to address both C++98/03 and C++11 (and beyond).


std::cin is an instance of a std::istream. That class provides two overloads that pertain to this question.

  • operator >> reads data from the stream into the target variable if that is possible. If the immediate contents of the stream cannot be translated into the type of the target variable, the stream is instead marked as invalid and the target variable is left untouched. Regardless of the success/failure of the operation, the return value is a reference to the stream.
  • Either operator void*() (pre-C++11), which converts the stream reference to a void* pointer, or explicit operator bool() (C++11), which converts the stream reference to a boolean. The result of this conversion is a non-null pointer (pre-C++11) or true (C++11) if the stream is valid, but the null pointer (pre-C++11) or false (C++11) if the stream isn't valid.

An if statement needs either a boolean, an integer, or a pointer as the quantity to be tested. The result of std::cin >> x is a reference to an istream, which is none of the above. However, the class istream does have those conversion operators which can be used to transform the istream reference to something usable in an if statement. It is the version-specific conversion operator that the language uses for the if test. Since failure to read marks the stream as invalid, the if test will fail if the read didn't work.

The reason for the more convoluted operator void* conversion member prior to C++11 is that it wasn't until C++11 that the already existing explicit keyword was extended to apply to conversion operators as well as constructors. A non-explicit operator bool() would have presented far too many opportunities for programmers to shoot themselves in the foot. There are problems with operator void*() as well. The "safe bool idiom" would have been a fix, but simply extending explicit accomplished exactly what the safe bool idiom accomplishes, and without having to use a lot of SFINAE magic.


cin is a (global) variable of type istream, not a function.

The istream class overrides the >> operator to perform input and return a reference to the object you called it on (cin).


cin is variable in std namespace.

operator>> return reference to cin, because of it you can write: cin >> a >> b, instead of cin >> a; cin >> b;


The answers above are informative. Here I just give an extra comment.

std::cin is an object of class istream and represents the standard input stream (i.e. the keyboard) which corresponds to stdin in C stream.

cin >> x would firstly read an int from the standard input stream and assignment it to x. After that return a self reference to cin. So the return value of function call cin >> x is still cin.

So from the point of if condition, if(cin) and if(cin >> x) resemble each other. The standard IO Library defines a function for the stream like this (depends on implementation):

explicit operator bool() const; // C++11

or

operator void*() const; //C++98, C++2003

From this two declarations, we know they cast the stream type directly or indirectly(through void* pinter to bool which is obvious) to bool type.

Within this two functions, they depends on some basic IO steam statuses(class fields) to determine whether return false or true (for void* case, it is nullptr or not).

cin is an instance of class istream which inherits the casting-to-bool function. So it works!


because the result of the expression

cin >> x

evaluates to

cin

after the stream is read.


1) cin is an instance of istream, see http://www.cplusplus.com/reference/iostream/cin/.

2) the >> operator of istream will return its left operand, in this case it is cin, see http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/. This operator will set failbit on if no characters were extracted from the cin, in case the reader has finished EOF so there will be no more character to read.

3) As of the 2) above, when the condition is evaluated after the reading operation, if (cin >> x) should be like if (cin), refer to this link http://www.cplusplus.com/reference/ios/ios/operator_bool/ you will see that, this if block will return:

  • A null pointer if at least one of failbit or badbit is set. Some other value otherwise (for C++98 standard).

  • The function returns false if at least one of these error flags is set, and true otherwise. (for C++11 standard)