cin
in three places, instead of one// Technically correct, BUT NOT GOOD style. int sum = 0; int x; cin >> x; // BAD idiom for input. Taken from while (cin) { // (poor) Pascal I/O. Do not use in C++. sum = sum + x; cin >> x; } cout << sum;
cin
loop for more discussion of this.
// This is a better idiom. It groups all I/O in
// one statement, not three as above.
// It's more readable, and therefore more
// more maintainable and less likely to have bugs.
int sum = 0;
int x;
while (cin >> x) {
sum = sum + x;
}
cout << sum;