undeclared identifier

I'm getting this error message "error C2065: 'calaverage' : undeclared identifier' I can't figgure out why. I gave calaverage a value in line 11.



#include <iostream>
using namespace std;
const int MAX = 10;

double calculateaverage(double calarr)
{
for (int i = 0; i < MAX; i++)
{
calaverage = calaverage + calarr;
}
calaverage = calaverage/MAX;
return calaverage;
}


int main()
{
double arr[MAX]; //declare array variable
double average = 0;
int i;
cout << "Enter 10 numbers: " << endl; //user input 10 numbers
for (i = 0; i < MAX; i++)
{
cin >> arr[i];
}
calculateaverage (arr[i]);

average = calaverage;

cout << "The average of the ten numbers is " << average << "." << endl;
return 0;
}
You never declared the data type of the variable.
I tried to declare it as a double, but i'm getting the same error. am I putting it in the wrong spot

#include <iostream>
using namespace std;
const int MAX = 10;

double calculateaverage(double calarr, double calaverage)
declare calaverage

double calculateaverage(double calarr[])
{
for (int i = 0; i < MAX; i++)
{
!!!!double!!!! calaverage = calaverage + calarr[i] ;
}
calaverage = calaverage/MAX;
return calaverage;
}

also do

int main()
{
double arr[MAX]; //declare array variable
double average = 0;
int i;
cout << "Enter 10 numbers: " << endl; //user input 10 numbers
for (i = 0; i < MAX; i++)
{
cin >> arr[i];
}

average = calculateaverage (arr);


cout << "The average of the ten numbers is " << average << "." << endl;
return 0;
}
Last edited on
To work with your code it needs to be declared globally.
I have tried it it bith way

#include <iostream>
using namespace std;
const int MAX = 10;
double calaverage;

and

double calculateaverage(double calarr[])
{
for (int i = 0; i < MAX; i++)
{
!!!!double!!!! calaverage = calaverage + calarr[i] ;

and it didn't work, I'm new to this as you can tell, but to declare something globally, I delcared it in the begining under (const int max= 10) am i doing sometime wrong?
If you declare it globally you only need

calaverage = calaverage + calarr[i];

with the for loop. If you leave in "double" in front of the line above, it won't work like you want it to.
you should have deleted the ! . I just wanted to show you where you should put double

so write this


double calculateaverage(double calarr[])
{
for (int i = 0; i < MAX; i++)
{
double calaverage = calaverage + calarr[i] ;
}
calaverage = calaverage/MAX;
return calaverage;
}


int main()
{
double arr[MAX]; //declare array variable
double average = 0;
int i;
cout << "Enter 10 numbers: " << endl; //user input 10 numbers
for (i = 0; i < MAX; i++)
{
cin >> arr[i];
}

average = calculateaverage (arr);


cout << "The average of the ten numbers is " << average << "." << endl;
return 0;
}
Last edited on
Hey guys thanks for your help, i finally got it to compile.
Topic archived. No new replies allowed.