C++写一个管理员增加职员信息
时间: 2023-07-13 12:07:40 浏览: 44
以下是一个简单的 C++ 示例代码,实现了管理员增加职员信息的功能:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Employee {
string name;
int age;
string position;
};
void addEmployee(Employee employees[], int& numEmployees) {
Employee newEmployee;
cout << "请输入新员工的姓名:";
getline(cin, newEmployee.name);
cout << "请输入新员工的年龄:";
cin >> newEmployee.age;
cin.ignore(); // 必须忽略输入缓冲区中的换行符
cout << "请输入新员工的职位:";
getline(cin, newEmployee.position);
employees[numEmployees] = newEmployee;
numEmployees++;
cout << "新员工已成功添加!" << endl;
}
int main() {
const int MAX_EMPLOYEES = 100;
Employee employees[MAX_EMPLOYEES];
int numEmployees = 0;
char choice;
do {
cout << "请选择要执行的操作:" << endl;
cout << "1. 增加职员信息" << endl;
cout << "2. 显示所有职员信息" << endl;
cout << "3. 退出程序" << endl;
cin >> choice;
cin.ignore(); // 必须忽略输入缓冲区中的换行符
switch (choice) {
case '1':
addEmployee(employees, numEmployees);
break;
case '2':
for (int i = 0; i < numEmployees; i++) {
cout << "姓名:" << employees[i].name << endl;
cout << "年龄:" << employees[i].age << endl;
cout << "职位:" << employees[i].position << endl;
cout << endl;
}
break;
case '3':
cout << "程序已退出。" << endl;
break;
default:
cout << "无效的选择,请重新输入。" << endl;
break;
}
} while (choice != '3');
return 0;
}
```
该程序使用结构体 `Employee` 存储职员信息,其中包括姓名、年龄和职位。管理员可以选择增加新的职员信息,程序会提示管理员输入新员工的姓名、年龄和职位,并将其添加到 `employees` 数组中。管理员还可以选择显示所有职员信息,程序会遍历 `employees` 数组并输出每个职员的信息。程序采用菜单式交互方式,直到管理员选择退出程序为止。