Thursday, July 30, 2009

Addition of integers in C++?

I am trying to write a program that takes in 5 numbers from a user and then add the numbers to get the sum.


I declared


int a,b,c,d,e,sum; so that those would be my varibles.


I get all the numbers typed in in the program but the addition part makes no sense.


I tried


%d=%d+%d+%d+%d+%d, %26amp;sum, %26amp;a, %26amp;b, %26amp;c, %26amp;d, %26amp;e;


I get a bunch of errors saying syntax error found. found 'd' expecting";"


Your help is greatly appreciated.

Addition of integers in C++?
You seem to be mistaking the printf statements of "%d=%d+%d+%d+%d+%d", %26amp;sum, %26amp;a, %26amp;b, %26amp;c, %26amp;d, %26amp;e being valid for if and for statements, etc. The printf statements only work with printf.





Similar to your other question, you only need to use the variables themselves.





sum = a + b + c + d + e;





You will probably only want to display the sum, so use


printf( "%d", sum);
Reply:What are you using to comile? Visual studio or? Try www.programmersheaven.com for some help.
Reply:what is this % sign for? %26amp; sign means the address of a variable in memory.





here is what you may want:





int main()


{


int a,b,c,d,e,sum;





// the part which takes in 5 numbers





sum=a+b+c+d+e;





// print out


return 0;


}
Reply:is that supposed to be a printf? lose the address refs in your variables. (get rid of the "%26amp;"s)





sum = a+b+c+d+e;


printf("%d = %d + %d + %d + %d + %d", sum, a, b, c, d, e);

dracaena

No comments:

Post a Comment