1. ## Loop problem

Hi, first of all i have to say Im a little embarrassed of making this question because Ive been reading a lot of posts out there, and they are really advanced topics. This is really simple.
I'm sure that the solution has to be also very simple, but I've been thinking about it and i just cant find it.
ok here it goes:

I have to add the numbers between 1 and 10 using a loop. I know how to do the loop, what i dont know is how add the numbers between 1 and 10.

I repeat that I know this has to be really stupid but i would appreciate some help.

Thanks.

2. Code:
```#include <iostream>

int main()
{
int num1, num2, num3;

num2 = 5;
num3 = 2;
num1 = num2 + num3;

std::cout << num3 << std::endl;

return 0;
}```

3. Thanks. but ive tried your code, and this isnt what im looking for. What i want to do is to add ALL the numbers between 1 and 10 using loop.

4. Originally Posted by robwhit
Code:
```#include <iostream>

int main()
{
int num1, num2, num3;

num2 = 5;
num3 = 2;
num1 = num2 + num3;

std::cout << num3 << std::endl;

return 0;
}```

5. what code do you have so far? you said that you knew how to do the loop part.

6. I dont see any loop there. Imagine a want to do it with a while loop. How would I do it?

7. ok this is what i got:

Code:
```#include <iostream>

using namespace std;

int main()
{

int x;

x = 0;

while ( x <= 10)
{
?????
}```

8. Originally Posted by Tesnik
How would I do it?
I'd do it by using a counter variable and then using another variable and add successively to the variable the value of the counter each time the loop repeats.

the code should be indented and in between braces like this:
Code:
```#include <iostream>

using namespace std;

int main()
{
int x;

x = 0;

while ( x <= 10)
{
?????
}
return 0;
}```
have you done for loops yet? they would usually be used in this situation, but you can use a while loop too.

here's the general layout in case it isn't clear:
Code:
```    while ( x <= 10)
{
// add y + x to y
}```
http://www.cprogramming.com/tutorial/lesson3.html

9. How's this?
Code:
```#include <iostream>
using namespace std;

int AddNums( int begin, int end )
{
if ( begin == end )
{
return begin;
}
else
{
return end + AddNums( begin, end - 1 );
}
}

int main()
{
cout << AddNums( 1, 10 );
return 0;
}```

10. ok umm, from your post ive tried this:

Code:
```#include <iostream>

using namespace std;

int main()
{
int x;
int y;

x = 1;
y = 1;/*counter*/

while( x <= 10 )
{
cout<< x <<"\n";
y++;
x = x + y;

}

system("pause");
return 0;

}```
but... the output is this:

1
3
6
10

and I dont want that

11. oops i wrote to slow

cpjust THATS what i wanted!!

thanks both of you

Ive been studying C++ from this page (which is great) for about two weeks, and Ive done simple programs a little bit more complicated than this, thats why im so surprised that i couldnt find a solution for this, i was beggining to think i was stupid. Actually I havent found a solution, somebody else found it, so i think that makes me stupid

thanks again

12. the counter is the one that goes 1 2 3 4 5 6 7 8 9 10. When you write this line:

x = x + y;

then it doesn't do that. you want:

y = x + y;

and switch the lines

y++;
y = x + y;

to

y = x + y;
y++;

13. ok thanks very much

14. You can also try this:
Code:
```#include <iostream>
using namespace std;

int main()
{
int n = 0;
int i = 10;

loop:
n += i;

if ( --i > 0 )
goto loop;

cout << n << endl;

return 0;
}```

15. wow haha i think thats a little more advanced. I understood the other code better

I haven't seen the loop: thing.
and the --i... dont know what it is

but i will learn.

thanks again