定义一个结构体类型,包括职工的如下信息:职工号、姓名、年龄、工资和职称,输入 3 个人的信息,然后输出
时间: 2023-12-19 07:20:53 浏览: 140
员工的一些基本信息的输入和输出
好的,以下是定义结构体类型、输入和输出的代码:
```c++
#include <iostream>
#include <string>
using namespace std;
// 定义结构体类型
struct Employee {
int id; // 职工号
string name; // 姓名
int age; // 年龄
double salary; // 工资
string title; // 职称
};
int main() {
// 输入 3 个人的信息
Employee emp1, emp2, emp3;
cout << "请输入第 1 个人的信息:" << endl;
cout << "职工号:";
cin >> emp1.id;
cout << "姓名:";
cin >> emp1.name;
cout << "年龄:";
cin >> emp1.age;
cout << "工资:";
cin >> emp1.salary;
cout << "职称:";
cin >> emp1.title;
cout << "请输入第 2 个人的信息:" << endl;
cout << "职工号:";
cin >> emp2.id;
cout << "姓名:";
cin >> emp2.name;
cout << "年龄:";
cin >> emp2.age;
cout << "工资:";
cin >> emp2.salary;
cout << "职称:";
cin >> emp2.title;
cout << "请输入第 3 个人的信息:" << endl;
cout << "职工号:";
cin >> emp3.id;
cout << "姓名:";
cin >> emp3.name;
cout << "年龄:";
cin >> emp3.age;
cout << "工资:";
cin >> emp3.salary;
cout << "职称:";
cin >> emp3.title;
// 输出 3 个人的信息
cout << "第 1 个人的信息:" << endl;
cout << "职工号:" << emp1.id << endl;
cout << "姓名:" << emp1.name << endl;
cout << "年龄:" << emp1.age << endl;
cout << "工资:" << emp1.salary << endl;
cout << "职称:" << emp1.title << endl;
cout << "第 2 个人的信息:" << endl;
cout << "职工号:" << emp2.id << endl;
cout << "姓名:" << emp2.name << endl;
cout << "年龄:" << emp2.age << endl;
cout << "工资:" << emp2.salary << endl;
cout << "职称:" << emp2.title << endl;
cout << "第 3 个人的信息:" << endl;
cout << "职工号:" << emp3.id << endl;
cout << "姓名:" << emp3.name << endl;
cout << "年龄:" << emp3.age << endl;
cout << "工资:" << emp3.salary << endl;
cout << "职称:" << emp3.title << endl;
return 0;
}
```
运行程序后,依次输入每个人的信息,即可输出他们的信息。
阅读全文