944,163 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7341
  • C++ RSS
Mar 31st, 2005
0

Dynamic arrays inside a class?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
solthirsty is offline Offline
1 posts
since Mar 2005
Apr 1st, 2005
0

Re: Dynamic arrays inside a class?

Here is one example of a dynamic array inside a class.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: cin.get issue
Next Thread in C++ Forum Timeline: Pleae help!!! Homework woes!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC