C++语言程序设计

下面我们再举一个结构数组的例子，它是例8-2修改版本，允许输入10个雇员的信息并存储在结构数组中。

| 例8-5

#include <iostream.h>
const int MAXIMUM_NAME_LENGTH = 50;
const int NUMBER_OF_EMPLOYEES = 10;
typedef char name_string[MAXIMUM_NAME_LENGTH];
struct employee
{
name_string name;
int age;
double salary;
};
void input_details(employee& this_employee);
void display(employee this_employee);
int main()
{
employee employee_list[NUMBER_OF_EMPLOYEES];
int count;
for (count = 0 ; count < NUMBER_OF_EMPLOYEES ; count++)
{
cout << "EMPLOYEE NUMBER " << (count +1) << ":\n";
input_details(employee_list[count]);
cout << endl;
}
for (count = 0 ; count < NUMBER_OF_EMPLOYEES ; count++)
display(employee_list[count]);
return 0;
}
void input_details(employee& this_employee)
{
cout << "Please enter the employee's last name: ";
cin >> this_employee.name;
cout << "Please enter the employee's age: ";
cin >> this_employee.age;
cout << "Please enter the employee's current salary: ";
cin >> this_employee.salary;
}
void display(employee this_employee)
{
cout << this_employee.name << " has age " << this_employee.age;
cout << " and salary " << this_employee.salary << ".\n";
}

从给出的程序可以看出：对结构数组的操作实际上是对每个数组元素进行操作，即对每个结构变量进行赋值和引用。需要注意的是：不能把一个结构数组元素整体进行输入输出，而相同结构类型的数组元素和变量之间可以相互赋值。