C++ Standard Input a series of numbers

Introduction

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 ww w .ja  v a  2  s.co  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;
}



PreviousNext

Related