cin
in a while
loopint sum = 0; int x; while (cin >> x) { sum += x; } cout << sum;
int sum = 0; int x; cin >> x; // BAD idiom for input. while (cin) { // Required by inadequate Pascal I/O. sum += x; // Should not be used in C++. cin >> x; } cout << sum;
while (cin)
test, depending
on whether there is whitespace after the last value. Think about it.
The issue is rather subtle - how does the I/O system know that you aren't going to
read the blanks after the last value? Well, it can't really know, so it makes
some assumptions. Not all systems make the same assumptions about how to handle
trailing whitespace after the last value.