Nesting control commands

I was wondering if anyone could explain this:
1
2
//exit if the loop if the total accumulated is 0
if (adder==0)

I am lost as to how you get adder to become 0??? I am learning cplusplus from a book and I am just confused by this. Here is the full main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace 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;
        }
}
closed account (SECMoG1T)
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 .
Thanks Andy1992.
Topic archived. No new replies allowed.