Excel-Like Sum function

Here is my code:

#include <iostream>
#include <stdarg.h>

using namespace std;
int sum( int amount, ...)
{
int i,val,total=0;
va_list vl;
va_start(vl,amount);

for (i=0;i<amount;i++)
{
val=va_arg(vl,int);
total += val;
}
va_end(vl);
return total;
}

int main ()
{
int m;
m= sum(1,2,3); // it must be 6, but why 2?
cout << m;

cin.get();
return 0;
}

The result must be 6, but it is 2;

Please help

thanks
Unspecified arguments should be a last resort. When you can't use arrays, vectors, inheritance, etc. only then is okay to use unspecified arguments.

Put your parameters inside an array and pass to the function a pointer to the array.

Now, about your question: if you read the parameter list you yourself defined, you will notice than the first parameter is the number of extra parameters being passed. Now look at your function call and notice that the first parameter is 1, the second is 2, and the third is 3.
Tell me, now: how much is 0+2?
Last edited on
Sorry, I am new to C++. I've pick up it from one website reference then i want to make for my own function Sum(1,2,3,... so on) the same as function in Excel.

The example from http://www.cplusplus.com/reference/clibrary/cstdarg/va_arg.html is ok. I just modify something.

I wonder why.

Thanks
You didn't read the text closely. It says:

"This FindMax functionn takes as its first parameter the amount of additional arguments it is going to get."

That is what you copied. The first parameter to your sum() function is the number of additional arguments. You call sum( 1, 2, 3 ), which means you are telling your function that there is 1 number to add, and that number is 2, since it is the next parameter. So as helios has said, 2 plus nothing is still 2.

If you call sum( 3, 1, 2, 3 ) then your function will add 1 + 2 + 3 == 6.

However as helios also suggested a far better approach is to pass an array of integers.

1
2
3
4
5
6
int sum( int a[], int count ) {
    int summation = 0;
    for( int i = 0; i < count; ++i )
       summation += a[ i ];
    return summation;
}


And there are many better ways to add numbers than this.
Yes, I understand. However, here is my further question.

Hi jsmith!, your code works fine, but what function i need is Sum(1,2,3) = 6. Is there any ways?


Thanks
Not with unspecified parameters. You'd end up with a plain old int sum(int,int,int) function.

EDIT: Oh, unless you're willing to do some string parsing.
Last edited on
Oh sorry that my question is not clear, here it is:

i need one function that can pass unlimited number of parameter like sum function in ms-excel.

The following either one is ok:

Sum(1,2)
Sum(1,2,3)
Sum(1,2,3,4,5,6,7,8)

Thanks
Yes, I understood you perfectly.

It's impossible. You need SOME way to tell the function in advance how many parameters you're passing. This value MUST be the first parameter, no matter what.
This is the nearest that can be done (which you already have):
sum(1,1)==1
sum(2,1,2)==3
sum(3,1,2,3)==6
sum(4,1,2,3,4)==6

There's no point in doing what you ask, anyway, because the parameters can't be changed dynamically (during run-time).
Just pass a pointer to an array or to a vector. That's all you need and all you'll ever need, trust me.
Last edited on
Topic archived. No new replies allowed.