Arrays of Structures

Suppose we wanted to hold data for a company with 100 empolyees:
Employee emp1, emp2, emp3, emp4 . . . emp100;    
This would be tedious and extremely inefficient. We would use an array of Employee structures:
Employee employees[100];
To display the information for an individual employee, we would include an index with the employees variable, much like arrays. This displays the 10th element:
cout << employees[9].firstName << endl;
cout << employees[9].lastName << endl;
cout << employees[9].middleInitial << endl;
cout << employees[9].salary << endl;
cout << employees[9].age << endl;
cout << employees[9].years << endl;
Of course, arrays are meant to allow us to use a loop something like this:
for (int index = 0; index < NUM_EMPLOYEES; index++)
{
  cout << "Employee #" << index << endl;
  cout << employees[index].firstName << endl;
  cout << employees[index].lastName << endl;
  cout << employees[index].middleInitial << endl;
  cout << employees[index].salary << endl;
  cout << employees[index].age << endl;
  cout << employees[index].years << endl;
}
Previous page
Next page

Back to Lesson 14 Index
Back to Outline