Write a basic while loop with a function code in the program that sums a stream of integers (input from the keyboard) and continues to do so while the integer entered is not -999. Ask the user to enter values in the range of 1 to 10000 or -999 to quit.
#include <iostream>
using namespace std;
long ans = 0;
int main()
{
cout << "Enter an integer in the range of 1 to 10000 or -999 to quit\n";
while (true) {
long n;
cin >> n;
if (n == -999) {
cout << ans;
break;
}
ans += n;
}
return 0;
}
Sample input:
1
2
3
-999
Sample output:
6
Comments
Leave a comment