Thread: Function with Array Structure

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    11

    Function with Array Structure

    Hi, how you all doing? I currently enroll C++ programming class in some community college, this is my second C++ class. I still have many thing to learn about it.

    I am now facing with some class assignment problem that I couldn't go any further, would you guys please help me out here?

    It gives me some ERRORs:
    -expected constant expression
    -cannot allocate an array of constant size 0
    -'emp' : unknown size

    Here is my program:

    Code:
    #include <iostream>
    #include <cstring>
    #include <iomanip>
    using namespace std;
    
    //STRUCTURE AND UNION//////////////////////////////////////////////////////////////////////
    
    union payRate
    {
    	long salary;
    	float hour;
    	int commission;
    };
    
    struct Employee
    {
    	char ID[4];
    	char name[36];
    	char payStatus[20];
    	float grossPay;
    };
    ////////////////////////////////////////////////////////////////////////////////////////////
    //FUNCTIONS/////////////////////////////////////////////////////////////////////////////////
    
    int getEmployeeData(Employee*, int);
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    
    //MAIN (STARTING POINT)/////////////////////////////////////////////////////////////////////
    
    int main()
    {	
    
    	int numEmployee;
    	Employee emp[numEmployee];
    	
    
    	cout<<"Enter maximum number of employees to enter: ";
    	cin>>numEmployee;
    
    	getEmployeeData(emp, numEmployee);
    
    	return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////
    
    //ALL THE FUNCTION DEFINITIONS//////////////////////////////////////////////////////////////
    
    int getEmployeeData(Employee* e, int numEmployee2)
    {
    	for(int loop=0; loop<numEmployee2; e++)
    	{
    		cout<<"Enter employee id (Enter to quit): ";
    		cin.ignore();
    		cin.getline(e->ID,4);
    
    		cout<<"Enter name (Last, First): ";
    		cin.ignore();
    		cin.getline(e->name,36);
    
    		cout<<"Enter payroll status: ";
    		cin.ignore();
    		cin.getline(e->payStatus,20);	
    	
    	}
    
    	return 0;
    
    }
    Thank you very much
    JJ

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    I think I have to use "Dynamic Memory Allocation" to work with "Array Structure", but I don't really know how to put them together.

    Can someones please help me?
    JJ

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    int numEmployee;
    Employee emp[numEmployee];


    You can't tell the compiler to make an array of an undefined size. Here you declare an integer, but it has not been initialized. Then you attempt to use this value to supposedly make an array of X numbers of employees. Will not work.

    2 ways to correct this:

    1. Initialize the numEmployee variable with a value within the code or have the value input from the user or a file.

    2. This is the best way. Learn about dyamic memory allocation, aka using the heap, and how to use pointers.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    First of all thank you very much for your reply.

    Quote Originally Posted by MacNilly
    1. Initialize the numEmployee variable with a value within the code or have the value input from the user or a file.
    -Do you mean I have to initialize the "numEmployee" variable within Function? But then "Employee emp" will not become an "Array".

    Quote Originally Posted by MacNilly
    2. This is the best way. Learn about dyamic memory allocation, aka using the heap, and how to use pointers.
    -I am trying to use "Dynamic Memory Allowcation" but it doesn't work and it gave me like 104 errors!!! I really don't understand how to put DMA to work with Function Array.

    Would you please give me some example of using DMA with Functin Array?

    I appriciate very much
    JJ

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    All I'm saying is that you can't make an array of an undefined size. In your code, you have to have the user input the number of employees (numEmployee) BEFORE you attempt to create an array of numEmployee size.

    As far as using the heap, there are tutorials on here. Look for the "new" keyword.

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Static array
    Code:
    const int numEmployee = 5;
    Employee emp[numEmployee];
    Dynamic array
    Code:
    int numEmployee;
    Employee *emp;
    
    cout<<"Enter maximum number of employees to enter: ";
    cin>>numEmployee;
    
    emp = new Employee[numEmployee];
    // do all kind of stuff
    
    // when you're done, do the following
    delete [] emp;
    I hope I got it right; been awhile coding in c++.
    Last edited by alphaoide; 12-11-2005 at 12:08 AM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    11
    ^^ Thank you very much alphaoide and MacNilly^^
    JJ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  3. structure array function
    By linucksrox in forum C Programming
    Replies: 17
    Last Post: 06-16-2004, 11:27 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM