我从练习中得到了这段代码,我需要:
所以我使用向量完成了这个,这是我的代码:
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 #########"<<"\n\n";
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"<<"\n\n";
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 modified\n";
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 deleted\n";
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时,它会抛出“exercise.exe已停止工作”。
PS。我是C ++的初学者,这是我的第一个编程语言,遗憾的是......
答案 0 :(得分:0)
发生崩溃是因为您访问超出其目的的多个向量的成员。
例如:
for(i = 0; i<ids.size(); i++) {
average[i] = (grades1[i] + grades2[i] + grades3[i])/3;
}
在访问之前,您永远不会扩展平均数组。您确实扩展了成绩数组,但仅限于响应特定的用户输入。
每次添加学生时,您可能希望在所有阵列中添加条目。并且您应该在添加成绩之前询问学生ID,并将该案例视为修改。
更好的设计是使用从学生ID到结构的地图。
答案 1 :(得分:0)
作为载体
vector<double> average;
vector<double> approved;
vector<double> repproved;
已创建,其大小为0,即它们不包含任何元素,因此当您第一次访问上述向量的元素为平均[i]时,这不是在内存中,所以您必须先将平均值插入,
average.push_back((grades1[i] + grades2[i] + grades3[i])/3);
不是
average[i] = (grades1[i] + grades2[i] + grades3[i])/3;
与其他两个向量相同
批准和 重新批准
我希望你很清楚。