由于学生信息管理系统的复杂程度和功能需求不同,所以最简单的学生信息管理系统的代码也可能会有所不同。下面是一个简单的学生信息管理系统的C++代码示例,包括添加学生信息、显示学生信息、查询学生信息和退出程序的功能。

#include <iostream>
#include <string>
using namespace std;

struct student {
    string name;
    int age;
    string id;
};

const int MAX_STUDENTS = 100;
student students[MAX_STUDENTS];
int num_students = 0;

void add_student() {
    if (num_students >= MAX_STUDENTS) {
        cout << "Error: too many students." << endl;
        return;
    }
    student s;
    cout << "Enter student name: ";
    getline(cin, s.name);
    cout << "Enter student age: ";
    cin >> s.age;
    cout << "Enter student ID: ";
    cin >> s.id;
    cin.ignore(); // ignore the newline character
    students[num_students] = s;
    num_students++;
    cout << "Student added." << endl;
}

void display_students() {
    if (num_students == 0) {
        cout << "No students." << endl;
        return;
    }
    cout << "Name\tAge\tID" << endl;
    for (int i = 0; i < num_students; i++) {
        student s = students[i];
        cout << s.name << "\t" << s.age << "\t" << s.id << endl;
    }
}

void search_student() {
    if (num_students == 0) {
        cout << "No students." << endl;
        return;
    }
    string id;
    cout << "Enter student ID: ";
    cin >> id;
    for (int i = 0; i < num_students; i++) {
        student s = students[i];
        if (s.id == id) {
            cout << "Name\tAge\tID" << endl;
            cout << s.name << "\t" << s.age << "\t" << s.id << endl;
            return;
        }
    }
    cout << "Student not found." << endl;
}

int main() {
    while (true) {
        cout << "Menu:" << endl;
        cout << "1. Add student" << endl;
        cout << "2. Display students" << endl;
        cout << "3. Search student" << endl;
        cout << "4. Quit" << endl;
        int choice;
        cout << "Enter choice: ";
        cin >> choice;
        cin.ignore(); // ignore the newline character
        switch (choice) {
            case 1:
                add_student();
                break;
            case 2:
                display_students();
                break;
            case 3:
                search_student();
                break;
            case 4:
                cout << "Goodbye!" << endl;
                return 0;
            default:
                cout << "Invalid choice." << endl;
        }
    }
}

该代码使用了结构体来存储学生信息,并使用一个数组来存储学生信息。用户可以选择添加学生信息、显示学生信息、查询学生信息或退出程序。在添加学生信息时,程序会检查是否已经达到了最大学生数。在查询学生信息时,程序会根据用户输入的学生ID查找学生信息。

最简单的学生信息管理系统c++代码

原文地址: https://www.cveoy.top/t/topic/b3pA 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录