c++:学生记录程序

C++: Student Record Program

本文关键字:记录 程序 c++      更新时间:2023-10-16

我急需帮助。我应该使用结构体和向量创建一个学生记录。在程序中,我应该有:

  1. 对于给定的学生,通过姓名或学号标识(a)在记录中输入一个新学生。(假设没有完成任何课程。)(b)平均绩点(c)成绩单,即所修课程的清单,包括学分和成绩(d)记录新完成的课程

  2. 按姓名排序的所有修过某门课程的学生名单。

  3. 按平均成绩排序的所有学生及其平均成绩的列表,在试用期,即平均绩点<2.0 .

这是我到目前为止所拥有的…我似乎有麻烦的用户输入如何读取它到我的结构

using namespace std;
struct courses {
    string courseName;
    int courseNum;
    double credit;
    char grade;
};
struct students {
    string name;
    int id;
    vector<courses> c;
};
int main() {
    // variables
    string name;
    char selector;
    students s;
    courses d;
    vector<students> student;
    vector<courses> course;
    // (1) create a menu: (a) user input, (b) echo record (with overall gpa), (c) failed students
    do {
        // prompt for user input
        cout << "Enter Q to (Q)uit, (C)reate new student record, (S)how all record(s) on file, show students on (P)robation: ";
        cin >> selector;
        selector = toupper(selector);
        switch (selector) {

            // (a) ask and get for user input:
            // student info first
            // courses second
        case 'C':
            // variables within case C
            char answer;
            char answerAddAnotherCourseEntry;
            char answerToAnotherStudentRecord;
            do {

                cout << "Enter your name and student ID number: ";
                cin >> s.name >> s.id;
                student.push_back(s);
                do {
                    cout << "Do you want to create a student course entry ('y' or 'n')? ";
                    cin >> answer;
                    answer = toupper(answer);
                    cout << "Enter your course number, course name, grade received and credit worth: ";
                    cin >> d.courseNum >> d.courseName >> d.grade >> d.credit;
                    course.push_back(d);

                    cout << "Add another student course entry ('y' or 'n'): ";
                    cin >> answerAddAnotherCourseEntry;
                    answerAddAnotherCourseEntry = toupper(answerAddAnotherCourseEntry);

                } while (answer == 'N');
                cout << "Add another student record ('y' or 'n'): " << endl;
                cin >> answerToAnotherStudentRecord;
                answerAddAnotherCourseEntry = toupper(answerToAnotherStudentRecord);
            } while (answerToAnotherStudentRecord == 'N');
            break;

            // (b) echo record of vectors
            // sort by name
        case 'S':
            if (student.empty()) {
                cout << "nSorry, no records exist in the database.";
            }
            else
                for (int count = 0; count < student.size(); count++) {         //For Loop to Display All Records
                    cout << "Student name: " << student[count].name << "ID Number: " << student[count].id << endl;
                    count++;
                    // another for loop i think
                    for (int i = 0; i < course.size(); i++) {
                        cout << "Course info: " << " " << course[i].courseNum << " " << course[i].courseName << " " << course[i].credit << " " << course[i].grade << endl;
                        i++;
                    }
                }
            cout << endl;
            break;

            // (c) separate failed student into another vector
            // sort by gpa
        case 'P':
            break;


        } // bracket closing switch
    } // bracket closing do while
    while (selector != 'q' && selector != 'Q');     // first do while

我看到的问题是,你试图将用户输入直接存储在字符串中,例如在结构体s的变量name中:

cout << "Enter your name and student ID number: ";
cin >> s.name >> s.id;

不能编译。相反,您可以将名称存储在临时字符数组中,然后将其复制到s.name

char studentName[128];
cout << "Enter your name and student ID number: ";
cin >> studentName >> s.id;
s.name = studentName;

如果你想将输入直接存储在字符串中,也可以使用getline()。

此外,您可以尝试直接输出变量student[count].name(这是一个字符串)到cout:

cout << "Student name: " << student[count].name << "ID Number: " << student[count].id << endl;

首先将字符串转换为字符数组,使此编译:

cout << "Student name: " << student[count].name.c_str() << "ID Number: " << student[count].id << endl;