Dynamic arrays inside a class?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 1
Reputation: solthirsty is an unknown quantity at this point 
Solved Threads: 0
solthirsty solthirsty is offline Offline
Newbie Poster

Dynamic arrays inside a class?

 
0
  #1
Mar 31st, 2005
I think the best way for me to explain this is to show you guys what im trying to do.

#include <iostream>
class customer
{
	int CustID;

public:
	void CCustRecords(int NumOfCust)
	{
            x = NumOfCust;
	    for(int i = 0; i < x; ++i)
	    {
		cout << "Enter the Customers ID number. " << endl;
		cin >> CID;
		CID[i].CustID = CID;
            }
        }
};

void main()
{
        int NumOfCust, EmployeeID;
	Employee emp;
	customer cust;
                
	cout << "Welcome to the MED Recorde Managemant System" << endl;
	cout << "--------------------------------------------" << endl;
	cout << endl << "Authorized Personel Only" << endl;
	cout << "Please Enter your Employee ID code " << endl;
	cin >> EmployeeID;

	if(EmployeeID == emp.EmployeeID)
	{
	    cout << "Please enter the number of Customers you want" << endl;
	    cin >> NumOfCust;
            
            cust.CCustRecords(int NumOfCust);
	    customer *CID = new customer[NumOfCust];
            
            cout << endl << endl << endl;
            cout << "Access  Granted!" << endl;
	    cout << "----------------" << endl;

	    
		while(iface != 1)
		{  
		    cout << endl << "Please select one of the following" << endl;
		    cout << endl << "1)Creat Customer records" << endl;
                    cout << endl << "2)Exit Program" << endl;
		    			
		    int iface;
		    cin >> iface;

		    if(iface = 1)
		    {
			cust.CCustRecords(int NumOfCust);
			break;
		    }
                    else
                        cout << "Exiting Program" << endl;
			
	       }

	       delete [] CID;
	}
	else	
	    cout << "I'm sorry but you entered the wrong number" << endl;
}

I'm in school and need help trying to input data into the dynamic array thats inside a class. I made a test program that worked but use a structure inside the main function. I dont understand what i'm missing. If you guys could give me a few pointer in the general direction, certain general areas to study, it would help and a brief example but nothing in detail. Ill find those. Thanks for you time. The error i get is: error C2109: subscript requires array or pointer type
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,386
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 243
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Dynamic arrays inside a class?

 
0
  #2
Apr 1st, 2005
Here is one example of a dynamic array inside a class.
  1. #include <iostream>
  2.  
  3. class T
  4. {
  5. int size, *array;
  6. public:
  7. T(int mysize = 1) : size(mysize), array(new int[size])
  8. {
  9. std::cout << "size = " << size << '\n';
  10. }
  11. ~T() { delete[] array; }
  12. };
  13.  
  14. int main()
  15. {
  16. T a, b(5);
  17. return 0;
  18. }
  19.  
  20. /* my output
  21. size = 1
  22. size = 5
  23. */
From what you've shown, it looks a little inside-out; that is, it seems like you want a dynamic array of customers in main, rather than a dynamic array of whatever in a class.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC