7

In an example given in C++ Primer,

#include <iostream>
using namespace std;

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

}

How does (std::cin >> value) return true? And what is an "End Of File"? It seems that I must understand that term in order to understand my primary question.

Thanks!

Superex
  • 199
  • 1
  • 10
  • end of file? it means... end of the file. you've reach the end. the terminus, the though-shalt-not-pass point, there's nothing to see past here. – Marc B Nov 12 '15 at 15:07
  • @MarcB, funny how there is more to that :) there are two different things which stand for end-of-file in Windows world (at least, there were 15 years ago) – SergeyA Nov 12 '15 at 15:09
  • The answers on the similar question, only then with `cout`, should also help explain: [Do while loop with a cout statement](http://stackoverflow.com/q/33546984/2718186) – MicroVirus Nov 12 '15 at 15:15

2 Answers2

9

The overloaded operator>> function returns a reference to the stream itself, and the stream have an overloaded operator that allows it to be used in a boolean condition to see if the last operation went okay or not. Part of the "okay or not" includes end of file reached, or other errors.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • While 100% correct, not sure if OP knows what 'overloaded operator function' means. – SergeyA Nov 12 '15 at 15:10
  • I'm sorry. I don't know what 'overloaded operator function' means. – Superex Nov 12 '15 at 15:14
  • @Superex Stream classes doesn't have any "inate" ability to use the shift operators `>>` and `<<`, instead they *overload* those operator with their own functions. If you don't know about overloading (a concept that is very important in C++) then I suggest you pick up a beginner book or tutorial from [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Some programmer dude Nov 12 '15 at 15:40
  • Thanks for informing me that I have missed out an important concept in C++. By the way, I can't find a proper downloadable version of Programming: Principles and Practices Using C++ and I cannot afford the book. So I picked up C++ Primer. I am going very slowly and I go crazy to understand stuff I don't. I have some experience in Python and Javascript. Thanks again! – Superex Nov 12 '15 at 17:41
  • And what is "End of File"? How can an input be present but absent at the same time? – Superex Nov 12 '15 at 17:52
  • @Superex "End of file" is not any input, it's a state. Once you have gone past the last byte in the file you will be at the end of the file. The "going past" part is important. Just reading the last byte is not enough to trigger the "end of file" state, you actually have to attempt to read once at the end of the file and then the system will notices "oh but I can't read anything more" and set the flags as needed. That is [why iostream::eof inside a loop condition considered wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – Some programmer dude Oct 11 '16 at 11:01
0

C++ translates this line

while (std::cin >> value)

to something like

inline bool f(int v) {
  auto& i = std::cin >> v;
  return i.operator bool();
}

while( f(v) ) {

Why it translates to bool? Because while expects a boolean expression, so compiler search for the boolean conversion operator of the return of std::cin >> v.

What is a bool conversion operator? Boolean conversion operator translates the object to bool. If some part of the code expect to some type to work a as boolean (like casting) then this operator is used.

What is an operator? Is a function or method that override the behavior of some operationg expressions (+, -, casting, ->, etc)

Edwin Rodríguez
  • 1,229
  • 10
  • 19