Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用<;向量>;使用cin和for()循环C++;_C++ - Fatal编程技术网

C++ 使用<;向量>;使用cin和for()循环C++;

C++ 使用<;向量>;使用cin和for()循环C++;,c++,C++,我的练习中有一段代码,我需要: 添加一名学生,其姓名、id、教育程度和3个年级 修改学生的分数 删除学生 显示已批准学生的列表 显示一份经过重新认证的学生名单 显示完整的列表 我已经使用向量完成了这项工作,下面是我的代码: int resp = 0; vector<int> ids; vector<string> names; vector<string> levels; vector<double> grades1; vector<d

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

  • 添加一名学生,其姓名、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 #########"<<"\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;
            }
int resp=0;
向量ID;
载体名称;
媒介水平;
病媒等级1;
病媒等级2;
病媒等级3;
向量平均;
矢量批准;
向量重证明;
字符串名称,级别;
int-id,elim,id\u-delete,i=0;
双级=0,总和=0;
做{

cout发生崩溃是因为您访问了多个向量的成员,这些向量超出了它们的端点

例如:

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

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

average[i] = (grades1[i] + grades2[i] + grades3[i])/3;
和其他两个向量一样

批准和 重新批准


我希望你说得清楚。

粘贴整个代码。你知道如何调试吗?到目前为止你做了什么。当我输入5时,你的程序做的第一件事就是
average[0]=(grades1[0]+grades2[0]+grades3[0])/3;
,但是
average
是空的,因此没有
average[0]整个代码:在多个范围内调用变量<代码> i>代码>。您应该考虑将这里声明的变量重命名为代码> int id、elIM、IDyDelphi、i=0、,作为其他内容。主题外的建议:除非您有一个很好的理由不让我看不见,否则就要构造一个统一名称、级别、等级等的结构。让1个向量由该结构组成。例如:
struct student{int id;std::string name;std::string level;double grade 1;…}
std::vector students;
应该可以真正简化您的簿记。
average[i] = (grades1[i] + grades2[i] + grades3[i])/3;