[英]Using <vector> with cin and for() loop C++
我從我的練習中得到了這段代碼,我需要:
所以我使用向量完成了這個,這是我的代碼:
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 已停止工作”。
附注。 我是 C++ 的初學者,遺憾的是,它是我的第一門編程語言......
發生崩潰是因為您訪問了超出其末端的多個向量的成員。
例如:
for(i = 0; i<ids.size(); i++) {
average[i] = (grades1[i] + grades2[i] + grades3[i])/3;
}
在訪問它之前,您永遠不會擴展平均數組。 您確實會擴展成績數組,但僅限於響應特定的用戶輸入。
每次添加學生時,您可能希望在所有數組中添加條目。 並且您應該在添加成績之前詢問學生ID,並將這種情況視為修改。
更好的設計是使用從學生 ID 到結構的映射。
作為向量
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;
與其他兩個向量相同
批准和重新批准
我希望你很清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.