// line 5
int calc(int val, int qtd){ |
This does not do what you want.
val is an int. That means it is a
single integer. An array has multiple integers.
You cannot pass an array to a function in C++ ... at least not in the traditional sense. Instead, you pass a pointer to the first element of the array:
|
int calc(int* vals, int qtd)
| |
(Note the added * here... that makes it a pointer)
One you make that change you'll have another problem here:
1 2
|
// line 8
sum=sum+val;
| |
This keeps adding a
single value to the sum over and over... rather than adding each element in the array once. So basically this is a goofy way to do multiplication (sum will be set to val*qtd because you're just adding val qtd times).
Since we've changed val to vals and made it a pointer instead of a single array... we can now access elements through that pointer via the usual [bracket] operator:
After that the program should work.
The error you're seeing is on this line:
|
cout << "The sum of all numbers is :" << calc(val, qtd);
| |
The problem with this is that 'val' was an array... and you were passing it to a function which accepted a single integer. Now that we've changed calc so that it accepts a pointer instead of a single integer, this will have fixed itself (array names can be automatically cast to pointers to their first element).
=========================
On a side note.... this:
1 2
|
// line 17
int val[qtd];
| |
is nonstandard and may not work depending on whether or not your compiler supports it. You can't use non-const variables as array sizes in C++. Though many compilers support it as an extension because it's allowed in C.
But whatever
=========================