c++代码学生管理信息系统内容包括添加学生成绩,显示学生成绩记录,查找学生成绩记录,删除学生成绩记录,保存学生成绩记录,导出学生成绩记录
时间: 2023-10-08 19:08:37 浏览: 205
学生成绩管理系统 c++代码
以下是一个简单的学生管理信息系统的C++代码,包括添加学生成绩、显示学生成绩记录、查找学生成绩记录、删除学生成绩记录、保存学生成绩记录和导出学生成绩记录功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Student {
string name;
int score;
};
vector<Student> students;
void addStudent() {
Student s;
cout << "Enter student name: ";
cin >> s.name;
cout << "Enter student score: ";
cin >> s.score;
students.push_back(s);
cout << "Student added successfully." << endl;
}
void displayStudents() {
if (students.empty()) {
cout << "No students found." << endl;
return;
}
cout << "Students:" << endl;
for (int i = 0; i < students.size(); i++) {
cout << i+1 << ". " << students[i].name << " - " << students[i].score << endl;
}
}
void findStudent() {
if (students.empty()) {
cout << "No students found." << endl;
return;
}
string name;
cout << "Enter student name to find: ";
cin >> name;
for (int i = 0; i < students.size(); i++) {
if (students[i].name == name) {
cout << "Found student: " << students[i].name << " - " << students[i].score << endl;
return;
}
}
cout << "Student not found." << endl;
}
void deleteStudent() {
if (students.empty()) {
cout << "No students found." << endl;
return;
}
string name;
cout << "Enter student name to delete: ";
cin >> name;
for (int i = 0; i < students.size(); i++) {
if (students[i].name == name) {
students.erase(students.begin() + i);
cout << "Student deleted successfully." << endl;
return;
}
}
cout << "Student not found." << endl;
}
void saveStudents() {
if (students.empty()) {
cout << "No students found." << endl;
return;
}
ofstream outfile("students.txt");
if (!outfile.is_open()) {
cout << "Unable to open file." << endl;
return;
}
for (int i = 0; i < students.size(); i++) {
outfile << students[i].name << "," << students[i].score << endl;
}
outfile.close();
cout << "Students saved successfully." << endl;
}
void exportStudents() {
if (students.empty()) {
cout << "No students found." << endl;
return;
}
ofstream outfile("students.csv");
if (!outfile.is_open()) {
cout << "Unable to open file." << endl;
return;
}
outfile << "Name,Score" << endl;
for (int i = 0; i < students.size(); i++) {
outfile << students[i].name << "," << students[i].score << endl;
}
outfile.close();
cout << "Students exported successfully." << endl;
}
int main() {
int choice;
while (true) {
cout << "1. Add student" << endl;
cout << "2. Display students" << endl;
cout << "3. Find student" << endl;
cout << "4. Delete student" << endl;
cout << "5. Save students" << endl;
cout << "6. Export students" << endl;
cout << "7. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent();
break;
case 2:
displayStudents();
break;
case 3:
findStudent();
break;
case 4:
deleteStudent();
break;
case 5:
saveStudents();
break;
case 6:
exportStudents();
break;
case 7:
return 0;
default:
cout << "Invalid choice." << endl;
}
cout << endl;
}
}
```
在这个程序中,我们使用了一个 `vector` 来存储学生信息。用户可以选择添加、显示、查找、删除、保存或导出学生信息。添加和删除操作会更新 `vector` 中的数据,而保存和导出操作会将数据写入文件。程序还包括错误处理,例如在文件无法打开时给出错误提示。
阅读全文