i created this program and it works perfictly but how do i make it so all the numbers,that this loop prints out, add together?
heres the code

#include<iostream>
using namespace std;

int main()
{
    int i,n,a,b;
  b=1;
  n=1;
    i=100;
    a=100;
  
    while (n<=i){
          cout<<n*26*n<<endl;
          n=n+1;
          }
    
    system ("pause");
}
int sum = 0;

while ( n <= i ) {
  int save = n * 26 * n;

  sum += save;
  cout<< save <<endl;

  ++n;
}

oo thats cool um im wondering if u can send me a link 2 a tutorial that explains in detail how that works um i have the basic i dea but i just wana learn alittle more about it

What don't you understand?

well i just wana know how the += thing works

Generally I require you to read a book instead of wasting my time with a question that can be more quickly and easily researched than posting and waiting for me to reply. But, a += b is logically equivalent to a = a + b .

o ok but anyways the thing u gave me didnt do what i asked help for 2 do all it did was do the same thing i had b4

I gave you enough code to understand the solution and complete it. But since that seems to be beyond you, I'll slave over the keyboard for five whole seconds to add one extra line:

int sum = 0;

while ( n <= i ) {
  int save = n * 26 * n;

  sum += save;
  cout<< save <<endl;

  ++n;
}

cout<<"\nSum: "<< sum <<'\n';

o well nex time tell me its not compleated

>o well nex time tell me its not compleated
Sorry, I thought that maybe the fact that it wouldn't compile without more code was a tiny hint. :icon_rolleyes:

actually it did comple

>actually it did comple
Not as posted it didn't. If you copy that exactly into a source file with no extra framework and try to build, it'll fail.

o thats what u mean well i kept the variables i already had and copied that and took out my loop cuz it did the same exact thing except an extra function. so ya your right lol um ok well thanks for the help

o btw i still cant find in any tutorial or book the

+=

thing

o btw i still cant find in any tutorial or book the

+=

thing

Wow, that's pretty hard to believe, when the first hit on a simple Google search of "C++ arithmetic operators" is quite informative.

Huy. . take it easy guyz. . .

well i just wana know how the += thing works

Generally I require you to read a book instead of wasting my time with a question that can be more quickly and easily researched than posting and waiting for me to reply. But, a += b is logically equivalent to a = a + b .

o btw i still cant find in any tutorial or book the

+=

thing

Huy. . take it easy guyz. . .

Why? I thought Narue was being VERY polite. If the OP doesn't take the effort in reading the replies, why bother giving usefull replies at all?

Niek

Wow, that's pretty hard to believe, when the first hit on a simple Google search of "C++ arithmetic operators" is quite informative.

well thats just the thig i didnt know it was called an arithmetic operator otherwise i would have found it and ya i just searched it and i found usefull results so thank u i guess but pleas dont asume i know all thoughes little details

o btw i still cant find in any tutorial or book the

+=

thing

This is called compound statement, a shorthand.
e.g: x = x + 5; ----> x += 5;

The general notation is:

expression1 = expression1 operator expresion2

becomes

Expression1 operator = expression2

e.g.

y = y – z + 1 --------------> y -= z + 1
a = a / b --------------> a / = b

o icic thanks um but what i was really wondering how does that work in the loop it kinda hard for me 2 picture it

>y = y – z + 1 --------------> y -= z + 1
Be careful when jumping to conclusions. These two expressions are not equivalent:

y -= z + 1    is equivalent to y = y - ( z + 1 )
y = y - z + 1 is equivalent to y = ( y - z ) + 1

The two produce different values.

>but what i was really wondering how does that work in the loop it kinda hard for me 2 picture it
Just change sum += save to sum = sum + save if it makes things easier. sum is a running total starting at zero, so if the loop has four iterations and save is set to 1, 2, 3, and 4, respectively, those values are added to sum and after the loop, sum is 10.

o icic thanks um but what i was really wondering how does that work in the loop it kinda hard for me 2 picture it

i don't wanna be rude or whatever... but this is just the lamest way to waste our time...

you should want to read the book first... if that doesn't work, come to us... i can't imagine how a person that has read the book can come here to ask these questions... i mean... PLZ...

though, i will explain... any arithmetic operator will work EXACTLY THE SAME inside or outside any loop... so, y you have a "sum" variable created out of the loop and an assigned value of 0... (if you would have read the book, you would know...) "sum" would be your counter, or whatever you want to call it... so, what you have to do is

sum+=save

inside the loop just as Narue had pointed out several (many, indeed) threads ago...

It doesn't need a genius to figure that out...

shut the fuck up nichito im trying my best to learn this stuff im not wasting your guyses time it takes me a while to learn this stuff everybody learns diffrently and just because u can learn this stuff faster or already know it doesnt give u the right to shove it up my ass.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.