用 c++写一个学生信息管理系统
时间: 2023-12-17 10:53:30 浏览: 75
好的,下面给出一个简单的学生信息管理系统的C++代码,实现了学生信息的添加、修改、删除和显示功能:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Student {
int id;
string name;
int age;
string gender;
};
vector<Student> students;
void addStudent() {
Student student;
cout << "请输入学生ID:";
cin >> student.id;
cout << "请输入学生姓名:";
cin >> student.name;
cout << "请输入学生年龄:";
cin >> student.age;
cout << "请输入学生性别:";
cin >> student.gender;
students.push_back(student);
cout << "添加成功!" << endl;
}
void modifyStudent() {
int id;
cout << "请输入要修改的学生ID:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
cout << "请输入新的学生姓名:";
cin >> students[i].name;
cout << "请输入新的学生年龄:";
cin >> students[i].age;
cout << "请输入新的学生性别:";
cin >> students[i].gender;
cout << "修改成功!" << endl;
return;
}
}
cout << "未找到该学生ID!" << endl;
}
void deleteStudent() {
int id;
cout << "请输入要删除的学生ID:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
students.erase(students.begin() + i);
cout << "删除成功!" << endl;
return;
}
}
cout << "未找到该学生ID!" << endl;
}
void showStudents() {
cout << "学生信息列表:" << endl;
for (int i = 0; i < students.size(); i++) {
cout << "ID:" << students[i].id << ",姓名:" << students[i].name << ",年龄:" << students[i].age << ",性别:" << students[i].gender << endl;
}
}
int main() {
cout << "欢迎使用学生信息管理系统!" << endl;
while (true) {
cout << "请输入操作指令:1.添加学生 2.修改学生 3.删除学生 4.查看学生列表 5.退出" << endl;
int command;
cin >> command;
switch (command) {
case 1: addStudent(); break;
case 2: modifyStudent(); break;
case 3: deleteStudent(); break;
case 4: showStudents(); break;
case 5: return 0;
default: cout << "无效的操作指令!" << endl; break;
}
}
return 0;
}
```
这个系统使用了结构体来保存学生信息,使用 vector 来存储多个学生信息,具有一定的实用性。
阅读全文