May 15, 2010 at 5:05pm UTC
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;
}
May 15, 2010 at 5:10pm UTC
You never declared the data type of the variable.
May 15, 2010 at 5:29pm UTC
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)
May 15, 2010 at 5:48pm UTC
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 May 15, 2010 at 5:56pm UTC
May 15, 2010 at 6:16pm UTC
To work with your code it needs to be declared globally.
May 15, 2010 at 7:35pm UTC
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?
May 15, 2010 at 7:59pm UTC
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.
May 15, 2010 at 8:31pm UTC
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 May 15, 2010 at 8:32pm UTC
May 16, 2010 at 4:28am UTC
Hey guys thanks for your help, i finally got it to compile.