C++ examples for Statement:while
Input a series of numbers. Continue to accumulate the sum of these numbers until the user enters a 0.
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int main(int nNumberofArgs, char* pszArgs[]) { int accumulator = 0; cout << "Terminate the loop by entering a zero" << endl; while(true) {/*from w w w . j a v a 2 s. c o m*/ int nValue = 0; cout << "Enter next number: "; cin >> nValue; if (nValue < 0) { cout << "Negative numbers are not allowed\n"; continue; } if (nValue == 0) { break; } accumulator += nValue; } cout << "\nThe total is " << accumulator << endl; return 0; }