Answer to Question #216161 in C++ for mika

Question #216161

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.


1
Expert's answer
2021-07-11T14:38:30-0400
#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




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS