having problem with using function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
double sum( double myList[]){
	double result = 0;
	for (int i= 0; i<4 ; i++){
		result += myList[i];
	}
	return result;
}

int main (){
	double myList[] = {9.6,20,5,12.2};
	double total = sum(myList[0], myList[1], myList[2], myList[3]);
	cout <<"The sum between"<<myList[0]<<","<<myList[1]<<","<<myList[2]<<"and"<<myList[3]
	<<"is"<<total<<endl;
	return 0;
}


can anyone please help me fix this error??
double sum( double myList[]){ takes one parameter.
double total = sum(myList[0], myList[1], myList[2], myList[3]); is passing four parameters.

You can just:
double total = sum(myList[]);
if i use:
double total = sum(myList[]);

and it will occur
syntax error : ]
Is the error any more verbose than just 'syntax error'?
well..I'm using Visual Studio 2008 currently...and after compile the code it occur error :
error C2059: syntax error : ']'


and my code after rectify is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
double sum( double myList[]){
	double result = 0;
	for (int i= 0; i<4 ; i++){
		result += myList[i];
	}
	return result;
}

int main (){
	double myList[] = {9.6,20,5,12.2};
	double total = sum(myList[]);
	cout <<"The sum between"<<myList[0]<<","<<myList[1]<<","<<myList[2]<<"and"<<myList[3]
	<<"is"<<total<<endl;
	return 0;
}


as what u told...
What about changing the function definition to: double sum(double &myList[]){

Right now I'm just shooting in the dark; I'll need to review arrays in a minute, I don't use 'em much.
LOL....more errors occur as i change it to double sum(double &myList[]){
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
double sum( double myList[]){
	double result = 0;
	for (int i= 0; i<4 ; i++){
		result += myList[i];
	}
	return result;
}

int main (){
	double myList[] = {9.6,20,5,12.2};
	double total = sum(myList);
	cout <<"The sum between"<<myList[0]<<","<<myList[1]<<","<<myList[2]<<"and"<<myList[3]
	<<"is"<<total<<endl;
	return 0;
}
AWESOME!! thank you so much !!
Topic archived. No new replies allowed.