c++职工信息录入
时间: 2023-06-29 11:09:27 浏览: 81
以下是一个简单的C++程序,用于职工信息的录入:
```c++
#include <iostream>
using namespace std;
struct Employee {
int id;
string name;
int age;
string position;
};
int main() {
int num;
cout << "请输入职工人数:";
cin >> num;
Employee* employees = new Employee[num];
for (int i = 0; i < num; i++) {
cout << "请输入第 " << i+1 << " 个职工的信息:" << endl;
cout << "编号:";
cin >> employees[i].id;
cout << "姓名:";
cin >> employees[i].name;
cout << "年龄:";
cin >> employees[i].age;
cout << "职位:";
cin >> employees[i].position;
}
cout << "录入完成,职工信息如下:" << endl;
for (int i = 0; i < num; i++) {
cout << "编号:" << employees[i].id
<< "\t姓名:" << employees[i].name
<< "\t年龄:" << employees[i].age
<< "\t职位:" << employees[i].position << endl;
}
delete[] employees;
return 0;
}
```
这个程序定义了一个 `Employee` 结构体,包含职工的编号、姓名、年龄和职位。用户先输入职工人数,然后依次输入每个职工的信息,最后输出录入的职工信息。注意,为了避免内存泄漏,我们在程序结束前要释放 `employees` 数组所占用的内存。
阅读全文