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 ?

4
  • It's std::istream&, but this question is a duplicate. Commented Oct 20, 2015 at 12:51
  • Hm or not this one, but in general it has numerous answers. Commented Oct 20, 2015 at 12:52
  • stringstream or cin does not matter, answer holds for all std::istreams. Commented Oct 20, 2015 at 12:54
  • May be a stupid question. How could the return value terminate the while loop ? Is the return value zero if input is EOF ? Commented Oct 20, 2015 at 13:08

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How could the return value terminate the while loop ? Is the return value zero if input is EOF ?
@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.
0

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.