Luna
OSNN Senior Addict
- Joined
- 6 Jan 2004
- Messages
- 303
This is an assignment I've been working on that really has me stuck. Long story short, our professor has given us a application that is supposed to allow a user to Add, Remove, and Modify employees into an array. Most of the code is actually done for us (can't change it), but we need to complete the sections that write and read information from an array. Simple enough... except this array is a pointer?
There are two classes in this application. In the class 'Company', you can see we have a private entry for an array called employees where the information will be stored.
So since most of this is done for us, I jump down to the void Company::AddEmployee() function. At first I thought the following code would allow me to store information into this array:
When this line is reached however, it crashes. When I debug, it looks like there is no memory allocated for this array, or it's not defined yet. Am I missing something simple?
Without the use of pointers I have no problem storing the information directly into the variables defined in the Employee class (or even into an array that's not defined as *), as seen below. I just don't know how to get it into the array that was defined in the above Class. I'm sure there is a way to input it directly into the array.
There are two classes in this application. In the class 'Company', you can see we have a private entry for an array called employees where the information will be stored.
Code:
/////////////////////////////////////////////////////////////////////
// Define the class Employee with public attributes
// This class describes essentials of the employee.
class Employee
{
public:
int id; // Employee ID
double maxPayRate; // Maximum pay rate
int maxHoursToWork; // Maximum hours that they can work
};
/////////////////////////////////////////////////////////////////////
// This is the class Company. It contains data and methods for
// up to 10 employees.
class Company
{
public:
// Default constructor
Company() {nextIndex=0;};
// These are class methods to add, modify, delete and
// display the employees.
void AddEmployee();
void ModifyEmpoyee();
void DeleteEmployee();
void DisplayEmployees();
private:
// These is private class data
// This pointer array holds the data for all of the employees
Employee* employees[10];
// This is the next index that will be used in the
// employees array above.
int nextIndex;
};
Code:
cin >> employees[0]->id;
Without the use of pointers I have no problem storing the information directly into the variables defined in the Employee class (or even into an array that's not defined as *), as seen below. I just don't know how to get it into the array that was defined in the above Class. I'm sure there is a way to input it directly into the array.
Code:
void Company::AddEmployee()
{
Employee emp = {0, 0.0, 0};
cout << "Please Enter the Employees Information: ";
cout << "\n \nID: ";
cin >> emp.id;
so on... so forth...
}
Attachments
Last edited: