C++ Primer Plus Chapter 5 Exercise 2

c plus plusExercise 2 ask us to write a loop that does not terminate until it sees a zero. A Do-While loop will best serve our interest for this exercise. The loop will sum all user inputted integers until a zero is inputted. Here is my solution:

2. Write a program that asks the user to type in numbers. After each entry, the program
should report the cumulative sum of the entries to date. The program should terminate
when the user enters 0.

#include <iostream>

using namespace std;

int main()
{
int number;
int sum = 0;

do
{
cout << "Enter numbers: ";
cin >> number;

sum = sum + number;
cout << "Current sum is: " << sum << endl;
}
while (number != 0);

return 0;
}
Advertisements