# Cant figure out how to do this!

• 10-16-2001
Warped1
Cant figure out how to do this!
Im not too good at C++, plus, I am not good at anything. Anyways, I am trying to make a prog that takes a number from the user and then adds all the numbers that lead up to it together. For example

cin>>5 (num)

This is the part I dont know how to do. I know i have to use a loop, and I know how to make it count up to the number, but how do i get it to take the number it gets and put it in some kind of queue that will be added up later (0+1+2+3+4+5)?

cout<<15 (sum)

any help would be great.
• 10-16-2001
*pointer
If all you're doing is adding all of the numbers before the number entered then you can easily do this with a simple loop.
Code:

```int total; for ( int i = 0; i < num; i++ ) {     total += i; } cout << total;```
• 10-16-2001
Troll_King
Code:

```#include<iostream.h> int main() {         int num;         int sum = 0;         cout << "Enter a number: ";         cin >> num;         while(num > 0)         {                 sum += num;                 --num;         }         cout << "Sum is: " << sum << endl;         return 0; }```
This can be made into a function. Here is one solution.
• 10-16-2001
Warped1
ok, i understand now, i didnt know about that += what is that?
• 10-16-2001
DanMan
total += i

is the same as

total = total + i

It's just one of those short-hand things that makes life easier. You can use that "trick" for any normal operator, like:

total -= i; (total = total - i; )
total *=i; (total = total * i; )
total /=i; (total = total / i; )
• 10-16-2001
Warped1
ahh, i see