<vector> 与 cin 和 for() 循环C++一起使用



我有一段练习中的代码,我需要:

  • 添加包含姓名、ID、教育水平和 3 个年级的学生
  • 修改学生的成绩
  • 删除学生
  • 显示已批准学生的列表
  • 显示重复学生列表
  • 显示完整列表

所以我使用向量完成了此操作,这是我的代码:

int resp = 0;
vector<int> ids;
vector<string> names;
vector<string> levels;
vector<double> grades1;
vector<double> grades2;
vector<double> grades3;
vector<double> average;
vector<double> approved;
vector<double> repproved;
string name, level;
int id, elim, id_delete, i=0;
double grade=0, sum=0;
do {
    cout<<"######### OPTIONS MENU #########"<<"nn";
    cout<<"[1] Add a student"<<"n";
    cout<<"[2] Add grades of a student"<<"n";
    cout<<"[3] Modify grades of a student"<<"n";   
    cout<<"[4] Delete a student"<<"n";
    cout<<"[5] Show approved list"<<"n";
    cout<<"[6] Show repproved list"<<"n";
    cout<<"[7] Show the full list"<<"n";
    cout<<"[8] Exit"<<"nn";
    cin>>resp;
    switch(resp) {
        case 1:
            cout<<"Enter the student's name: n";
            cin.sync(); cin.clear();
            getline(cin, name);
            names.push_back(name);
            cout<<"Enter the student's id: n";
            cin>>id;
            ids.push_back(id);
            cout<<"Enter the student's school level: n";
            cin.sync(); cin.clear();
            getline(cin, level);
            levels.push_back(level);
            cout<<"Student added";
        break;
        case 2:
            cout<<"Enter the grade 1 of the student: n";
            cin>>grade;
            grades1.push_back(grade);
            cout<<"Enter the grade 2 of the student: n";
            cin>>grade;
            grades2.push_back(grade);
            cout<<"Enter the grade 3 of the student: n";
            cin>>grade;
            grades3.push_back(grade);
            cout<<"Grades added";
        break;
        case 3:
            cout<<"Enter the student's id you wish to modify grades on: n";
            cin>>id_delete;
            for(int i=0; i<ids.size(); i++) {
                if(id_delete==ids[i]) {
                    id_delete = i;
                    break;
                }
            }
            i=id_delete;
            cout<<"Enter the new grade 1 of the student: n";
            cin>>grade;
            grades1[i] = grade;
            cout<<"Enter the new grade 2 of the student: n";
            cin>>grade;
            grades2[i] = grade;
            cout<<"Enter the new grade 3 of the student: n";
            cin>>grade;
            grades3[i] = grade;
            cout<<"Grades modifiedn";
        break;
        case 4:
            cout<<"Enter the student's id you wish delete: n";
            cin>>id_delete;
            for(int i=0; i<ids.size(); i++) {
                if(id_delete==ids[i]) {
                    id_delete = i;
                    break;
                }
            }
            ids.erase (ids.begin()+(id_delete-1));
            names.erase (names.begin()+(id_delete-1));
            levels.erase (levels.begin()+(id_delete-1));
            grades1.erase (grades1.begin()+(id_delete-1));
            grades2.erase (grades2.begin()+(id_delete-1));
            grades3.erase (grades3.begin()+(id_delete-1));
            cout<<"Student deletedn";
        break;
        case 5:
            for(i = 0; i<ids.size(); i++) {
                average[i] = (grades1[i] + grades2[i] + grades3[i])/3;
            }
            for(i = 0; i<ids.size(); i++) {
                if(average[i] >= 7 && average[i] <= 10) {
                    approved[i] = average[i];
                } else {
                        repproved[i] = average[i];
                    }
            }
            for(i = 0; i<ids.size(); i++) {
                cout<<approved[i]<<endl;
            }
        break;
        case 6:
            for(i = 0; i<ids.size(); i++) {
                cout<<repproved[i]<<endl;
            }
        break;
        case 7:
            for(i = 0; i<ids.size(); i++) {
                cout<<approved[i]<<endl;
            }
            for(i = 0; i<ids.size(); i++) {
                cout<<repproved[i]<<endl;
            }

但是在选择选项 5、6 和 7 时,它会抛出"锻炼.exe已停止工作"。

附言。我是C++的初学者,这是我的第一门编程语言,可悲的是......

发生崩溃是因为您访问了超出其末端的多个向量的成员。

例如:

for(i = 0; i<ids.size(); i++) {
    average[i] = (grades1[i] + grades2[i] + grades3[i])/3;
}

在访问平均数组之前,您永远不会对其进行扩展。 您确实会扩展成绩数组,但只是为了响应特定的用户输入。

您可能希望在每次添加学生时在所有数组中添加条目。 在添加成绩之前,您应该要求提供学生证,并像修改一样对待这种情况。

更好的设计是使用从学生ID到结构的地图。

AS the Vectors

vector<double> average;
vector<double> approved;
vector<double> repproved;

创建时,它们的大小为 0 即它们不包含任何元素,因此当您第一次访问上述向量的元素作为 average[i] 时,这不在内存中,所以您必须首先将平均值插入为,

average.push_back((grades1[i] + grades2[i] + grades3[i])/3);

不像

average[i] = (grades1[i] + grades2[i] + grades3[i])/3;

与其他两个向量相同

批准和 重新批准

我希望你清楚。

最新更新