954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to add 2 numbers together constantly in c++?

hello i have my code here and i want to add two numbers together constantly

#include

using namespace std;

int main()
{

int a = 10;
int b = 10;


cout << a+b;

return 0;
}


how to make A+B add constantly?

dakerao
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

If you just want to keep outputting a+b you could use an infinite loop. something along the lines of

while(true)
{
    // code here
}


I have to ask why you want to do this?

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

oh no i need to keep adding a and b so like a+b = 20 and then add a and b again and it would be 40 but do that in a continuation i guess thats a loop but exactly how would i do that?

dakerao
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

So in your example a and b are both 10. You add them together and you get 20. Now you want to set a and b to 20 and do it again?

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

you have to reassign a and b to equal eachother in intervals while in a loop

like

while(a+b!=40)
{
a=a+b;
b=a;
}
imgregduh
Newbie Poster
4 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 

ahh ha thanks for that Imgreg :)

dakerao
Newbie Poster
24 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
So in your example a and b are both 10. You add them together and you get 20. Now you want to set a and b to 20 and do it again?

I think it's more on the line of adding the result of a + b to a + b, continually in a loop.

To the OP, you could have a third variable, sum, initialized to zero and do something like:

sum = sum + a + b;

In this case though, a and b wouldn't change.

Question, do you want a and b to change?

Chilton
Junior Poster
106 posts since Oct 2009
Reputation Points: 34
Solved Threads: 16
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: