C++语言程序设计

| 例8-4

#include<iostream.h>
const int MAXIMUM_NAME_LENGTH = 50;
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 first_employee;
input_details(first_employee);
display(first_employee);
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 << endl;
cout << this_employee.name << " has age " << this_employee.age;
cout << " and salary " << this_employee.salary << ".\n\n";
}

下面是给出的输入和输出的例子：
Please enter the employee's last name: Miller
Please enter the employee's age: 37
Please enter the employee's current salary: 89.45
Miller has age 37 and salary 89.45.
函数的返回值也可以是结构变量。例如，在例8-2的main函数中，可以将语句：
input_details(first_employee);
替换为：
first_employee = input_details();
并将input_details(...)函数实现作如下修改：
employee 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;
return this_employee;
}