read on:
Code:
#include <iostream>
using std::cout;
using std::cin;
class Average
{
//these should be private data members
private:
//for an array, you must either specify a size or use a
//pointer and dynamically allocate memory. I used a pointer:
double*numbers;
int x;
int numsmone;
public:
//functions should only be defined in the class body.
//your implementations should be somewhere else.
Average(int nums);
//this is a destrctor. we need it in this case because we
//dynamically allocated memory that needs to be freed up.
//when the class runs out of scope, this is automatically called
~Average();
double calculateMean();
};
//here's the implementation for the constructor. Usually constructors only
//initialize the class. putting this much functionality into your constructor
//should probably be avoided.
Average::Average(int nums)
{
numsmone = nums - 1;
//here I'm dynamically allocating the memory for the array
numbers=new double[nums];
for (x = 0; x < nums; ++x)
{
if (x == 0)
cout << "Enter the 1st number: ";
else if (x == 1)
cout << "Enter the 2nd number: ";
else if (x == 2)
cout << "Enter the 3rd number: ";
else
cout << "Enter the " << x << "th number: ";
cin >> numbers[x];
}
}
//this is the destructor. don't worry about calling it.
Average::~Average()
{
//this deallocates the memory from before.
delete[]numbers;
}
double Average::calculateMean()
{
double mean;
for (x = 0; x < numsmone; ++x)
mean = numbers[x] + numbers[x+1];
mean = mean / (numsmone + 1); //forgot a semicolon
return mean;
}
int main ()
{
int nums;
cout << "Enter the number of numbers you wish to average: ";
cin >> nums;
Average Avgone(nums);
//on this next line, you forgot the (), and you're supposed to use the
//dot (.) operator, not the scope resolution operator (::)
cout << "The mean is " << Avgone.calculateMean();
cin.get();
return 0;
}
I fix your logic though (it's wrong)