Inputting multiple numbers using cin

I am trying to figure out how this code is working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int number, sum = 0, count = 0;
    cout << "Enter 10 negative numbers:\n";

    while (++count <= 10) 
    {
        cin >> number;

        if (number >= 0)
        {
            cout << "ERROR: positive number"
                 << " or zero was entered as the\n"
                 << count << "th number! Input ends "
                 << "with the " << count << "th number.\n"
                 << count << "th number was not added in.\n";
            break;
        }

        sum = sum + number;
    }

    cout << sum << " is the sum of the first " 
         << (count - 1) << " numbers.\n";


I do not understand how the multiple numbers are being input into a single variable and how the sum is being calculated. (I can't find the answer in this book)
I know I am missing something basic here, but if someone could give me pointer in the right direction, I sure would appreciate it.

First thing to note about this code is that your inside a while loop.
count starts at 0.

so the loop loops 10 times

count = 1;
count = 2;
count = 3;
...
count = 9;
count = 10;
count = 11; //loop is broken here

for every iteration or cycle through the code there is one user input into one variable.

cin >> number;

number is not storing multiple numbers but the sum is storing all of the numbers added together.

so lets say you enter 1,2,3,4,5,6,7,8,9,10

count = 1; number = 1; sum = 0 + 1;
count = 2; number = 2; sum = 1 + 2;
count = 3; number = 3; sum = 3 + 3;
count = 4; number = 4; sum = 6 + 4;
count = 5; number = 5; sum = 10 + 5;
count = 6; number = 6; sum = 15 + 6;
count = 7; number = 7; sum = 21 + 7;
count = 8; number = 8; sum = 28 + 8;
count = 9; number = 9; sum = 36 + 9;
count = 10; number = 10; sum = 45 + 10;

sum = 55;

I hope this helps.

Thank you very much!
It's crystal clear now.
Topic archived. No new replies allowed.