C ++:学生记录计划

时间:2016-10-06 04:18:37

标签: c++ vector struct

我迫切需要帮助。我应该使用结构和向量创建学生记录。 在该计划中,我应该:

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

  2. 所有参加过特定课程的学生的名单,按名称排序。

  3. 所有学生及其平均成绩的平均排名,按成绩点平均排序, 谁正在试用,即平均分等级< 2.0。

  4. 这就是我到目前为止......我似乎遇到了用户输入的麻烦,如何将其读入我的结构

    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
    

1 个答案:

答案 0 :(得分:0)

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

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

这不编译。相反,您可以先将名称存储在临时char数组中,然后将其复制到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;

首先将字符串转换为char数组进行编译:

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