how do cin works ?
assume that a is int, if the the correct data is intered i.e. a 5 it becomes true, how ?Code:if (cin >> a) {
............
}
else {.........................
}
Printable View
how do cin works ?
assume that a is int, if the the correct data is intered i.e. a 5 it becomes true, how ?Code:if (cin >> a) {
............
}
else {.........................
}
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.
What happens is something like:
1) try to read input into int a
2) if it fails, cin is switched into a failed state
3) return cin after the operation
4) convert cin into something that can be evaluated in a boolean context. Probably cin is converted to void*: if it is in good state, the conversion returns the pointer to cin, else it returns NULL.
5) pointers can be evaluated: NULL is false, anything else is true.