Using the do…while() loop, continuously scan for random integers, but add up only all the positive integers and store the total in one variable.
The loop shall only be terminated when the inputted integer is zero. Afterwards, print out the total of all inputted positive integers.
#include <iostream>
int main() {
int total = 0;
int n;
do {
std::cin >> n;
total += n > 0 ? n : 0;
} while (n != 0);
std::cout << total;
return 0;
}
Comments
Leave a comment