#include <iostream>
#include <cstdio>
#include <cstdlib>
usingnamespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
//the outer loop
cout<<"This program sums multiple SERIES of numbers. Terminate each SEQUENCE by entering a negative number."
<<"Terminate the SERIES by entering two negative numbers in a row.";
//continue to accumulate sequences
int adder;
for(;;)
{
//start by entering the next sequence of numbers.
adder=0;
cout<<"start the next SEQUENCE\n";
for(;;)//loop forever
{
//fetch another number
int number=0;
cout<<"enter the next number";
cin>>number;
//if it's negative.
if(number<0)
{
//then exit
break;
}
//if positive, add the number.
adder+=number;
}
//exit the loop if the total is 0
if(adder==0)
{
break;
}
//output the result and start over.
cout<<"The total for this sequence is";
cout<<adder;
}
}
This is how it works , on the start of each outer loop we reset adder to zero , inside the inner loop we collect a series of numbers and sum them to adder only if they are greater than 0, if we encounter a value less than zero we exit the inner loop, if our sum is zero we exit our outer loop else we display the value and start over again.
how adder can be zero
When you begin the outer loop adder will be equal to zero, if you keyed in a negative value in the first iteration you'll exit the inner loop, on exit adder will be equal to zero .