Class Syntax Help

This is a discussion on Class Syntax Help within the C++ Programming forums, part of the General Programming Boards category; I'm doing exercises out of a book for self-learning purposes, and one of the exercises is: Design, implement and test ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Class Syntax Help

    I'm doing exercises out of a book for self-learning purposes, and one of the exercises is:

    Design, implement and test class Average. An object of type Average should accept values that are put into it. At any point in time it should be possible to retrieve their cumulative arithmetic average.

    Well, I think (or, at least, hope) that my math is right in the sense that this is what I would have to do in order to complete the calculations, but my compiler (Dev-C++) is giving me a ton of errors saying "invalid types double[int] for array subscript". I know this is referring to the loops where I have numbers[x], but it just gives me the error "invalid types double[double] for array subscript" when I try changing x to the type double. I really have no clue as to how to get around this:

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    
    class Average
    {
    public:
    	double numbers;
    	int x;
    	int numsmone;
    
    	Average	(int nums)
    	{
    		numsmone = nums - 1;	
    	
    		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];
    		}
    	}
    
    	double CalculateMean ()
    	{
    		double mean;
    		for (x = 0; x < numsmone; ++x)
    			mean = numbers[x] + numbers[x+1];
    		mean = mean / (numsmone + 1)
    		return mean;
    	}
    };
    
    int main ()
    {
    	int nums;
    	cout << "Enter the number of numbers you wish to average: ";
    	cin >> nums;
    	Average Avgone(nums);
    	cout << "The mean is " << Avgone::CalculateMean;
    	cin.get();
    	return 0;
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 02:17 PM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 12:54 PM
  4. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21