2

Consider the following code

int main(){ 
int sum = 0, value = 0;
while (std::cin >> value)
sum += value; 
std::cout << "sum is: " << sum << std::endl;
return 0;
}

What is the return value of >> operator, against which while loop is evaluated ? The program terminates on EOF input (Ctrl+Z for windows). Does that mean 0 is stored to cin in case of an EOF ? Does it have anything to do with ASCII value of EOF ?

Paul259
  • 49
  • 6

3 Answers3

1

It returns a reference to basic_ifstream:

basic_istream& operator>>

In the context of if, it converts to true unless std::ios_base::failbit or std::ios_base::badbit is set in its state.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • How could the return value terminate the while loop ? Is the return value zero if input is EOF ? – Paul259 Oct 20 '15 at 13:10
  • @Paul259, if the state of `cin` will be set to `failbit` of `badbit` (for example, if `cin >> some_integer_variable` reads something that is not a number), it will convert to `false` and the loop will end. – SingerOfTheFall Oct 20 '15 at 13:23
0

According to this reference, the return type is istream&.

Codor
  • 17,447
  • 9
  • 29
  • 56
0

It returns a non-const reference to std::cin.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157