while( sum.eof() == false )
{
// let us say sum.eof() is false at this point (the last read was successful;
// we have not reached end-of-file just yet;
sum >> number ;
// but when we try to read one more number, end-of-file is reached and the read fails
// sum.eof() == true at this point; but we have no way of knowing it; we are not checking for it
sum1 = sum1 + number ;
cout << number << " " ;
}
Check for failure (end-of-file) of input immediately after (instead of before) the attempted input
1 2 3 4 5
while( sum >> number ) // while we have successfully read in another number
{
sum1 = sum1 + number ;
cout << number << " " ;
}
Wow, thanks man, it works!!! Btw, if i put sum >> number; before the while(! eof()), the output would also be the same but your code is simple and shorter. Thanks again dude!