Continue to accumulate the sum of these numbers until the user enters a 0. - C++ Statement

C++ examples for Statement:break

Description

Continue to accumulate the sum of these numbers until the user enters a 0.

Demo Code

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    cout << "Terminate the series by entering two\n negative numbers in a row\n";

    int accumulator;
    for(;;)// ww w. j a v a  2  s. c  o  m
    {
        accumulator = 0;
        cout << "Start the next sequence\n";
        for(;;){
            int nValue = 0;
            cout << "Enter next number: ";
            cin  >> nValue;

            if (nValue < 0){
                break;
            }
            accumulator += nValue;
        }
        if (accumulator == 0)
        {
            break;
        }
        cout << "The total for this sequence is " << accumulator << endl << endl;
    }
    return 0;
}

Result


Related Tutorials