Use functions to break the inner loop into its own function - C++ Statement

C++ examples for Statement:break

Description

Use functions to break the inner loop into its own function

Demo Code

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

void displayExplanation(void)
{
    cout << "Terminate the series by entering an\n"
         << "empty sequence.\n"
         << endl;/*from  w  w  w .  ja  v  a2 s. co m*/
    return;
}

int sumSequence(void)
{
    int accumulator = 0;
    for(;;)
    {
        int nValue = 0;
        cout << "Enter next number: ";
        cin  >> nValue;

        if (nValue < 0)
        {
            break;
        }
        accumulator += nValue;
    }
    return accumulator;
}

int main(int nNumberofArgs, char* pszArgs[])
{
    displayExplanation();

    for(;;){
        cout << "Enter next sequence" << endl;
        int accumulatedValue = sumSequence();

        if (accumulatedValue == 0){
            break;
        }
        cout << "The total is " << accumulatedValue << endl << endl;
    }

    return 0;
}

Result


Related Tutorials