int n; cout << "请输入要删除的学生学号:"; cin >> n; for (int j = 0; j < i.v.size(); j++) {if (i.v[j].num == n) {_0843杨晓慧 s = i.v[j]; i.v.erase(i.v.begin() + j); // 将删除的学生记录转存到另一个删除库中 cout << "删除成功!" << endl; } } system("pause"); return;完善上述代码使其能够将删除的信息转存至F1.txt
时间: 2024-02-14 09:24:37 浏览: 57
int n;
cout << "请输入要删除的学生学号:";
cin >> n;
ofstream fout("F1.txt", ios::app); // 打开文件,以追加的方式写入
for (int j = 0; j < i.v.size(); j++) {
if (i.v[j].num == n) {
_0843杨晓慧 s = i.v[j];
i.v.erase(i.v.begin() + j);
fout << s.name << " " << s.num << " " << s.score << endl; // 将删除的学生记录写入文件
cout << "删除成功!" << endl;
break; // 找到一个就退出循环
}
}
fout.close(); // 关闭文件
system("pause");
return;
相关问题
检查一下代码有没有错误#include<iostream> #include<vector> using namespace std; class Student{ private: string name; int id; char gender; int age; float score; public: void addStudent(string n, int i, char g, int a, float s){ name = n; id = i; gender = g; age = a; score = s; } void deleteStudent(vector<Student>& vec, int i){ vec.erase(vec.begin()+i-1); } void modifyStudent(vector<Student>& vec, int i, string n, char g, int a, float s){ vec[i-1].name = n; vec[i-1].gender = g; vec[i-1].age = a; vec[i-1].score = s; } void queryStudent(vector<Student>& vec, int i){ cout<<"姓名:"<<vec[i-1].name<<endl; cout<<"学号:"<<vec[i-1].id<<endl; cout<<"性别:"<<vec[i-1].gender<<endl; cout<<"年龄:"<<vec[i-1].age<<endl; cout<<"成绩:"<<vec[i-1].score<<endl; } }; int main(){ vector<Student> stuVec; Student stu; stu.addStudent("张三", 1, 'M', 20, 90); stuVec.push_back(stu); stu.addStudent("李四", 2, 'F', 21, 85); stuVec.push_back(stu); stu.addStudent("王五", 3, 'M', 22, 95); stuVec.push_back(stu); int op, id; string name; char gender; int age; float score; while(true){ cout<<"请选择操作:\n1.添加学生\n2.删除学生\n3.修改学生信息\n4.查询学生信息\n5.退出\n"; cin>>op; if(op==1){ cout<<"请输入学生姓名:"; cin>>name; cout<<"请输入学生学号:"; cin>>id; cout<<"请输入学生性别:"; cin>>gender; cout<<"请输入学生年龄:"; cin>>age; cout<<"请输入学生成绩:"; cin>>score; stu.addStudent(name, id, gender, age, score); stuVec.push_back(stu); cout<<"添加成功!\n"; } else if(op==2){ cout<<"请输入要删除的学生学号:"; cin>>id; for(int i=0;i<stuVec.size();i++){ if(stuVec[i].id==id){ stu.deleteStudent(stuVec,i+1); cout<<"删除成功!\n"; } } } else if(op==3){ cout<<"请输入要修改的学生学号:"; cin>>id; for(int i=0;i<stuVec.size();i++){ if(stuVec[i].id==id){ cout<<"请输入学生姓名:"; cin>>name; cout<<"请输入学生性别:"; cin>>gender; cout<<"请输入学生年龄:"; cin>>age; cout<<"请输入学生成绩:"; cin>>score; stu.modifyStudent(stuVec,i+1,name,gender,age,score); cout<<"修改成功!\n"; } } } else if(op==4){ cout<<"请输入要查询的学生学号:"; cin>>id; for(int i=0;i<stuVec.size();i++){ if(stuVec[i].id==id){ stu.queryStudent(stuVec,i+1); } } } else if(op==5){ break; } else{ cout<<"输入有误,请重新选择!\n"; } } return 0; }
这段代码是一个学生信息管理系统的程序,包括添加学生、删除学生、修改学生信息和查询学生信息等功能。它使用了一个名为 Student 的类来表示学生信息,其中包括学生姓名、学号、性别、年龄和成绩等属性,并提供了相应的方法来修改和查询这些信息。在主函数中,使用了一个 vector 容器来存储所有的学生信息,并通过一个循环来不断接受用户的操作选择,直到用户选择退出程序。代码看起来没有语法错误,但是没有对用户的输入进行严格的检查和错误处理,可能会导致程序崩溃或者出现不可预料的错误。同时,它的主要目的是演示如何使用类和容器来实现一个简单的学生信息管理系统,具体实现细节还需要根据实际需求进行修改和完善。
请你说出void modify(Student* head) { int id; cout << "请输入要修改的学生学号:"; cin >> id; Student* p = head->next; while (p != NULL) { if (p->id == id) { cout << "请输入修改后的学生信息:\n"; cout << "姓名:"; cin >> p->name; cout << "年龄:"; cin >> p->age; cout << "性别:"; cin >> p->gender; cout << "成绩:"; cin >> p->score; cout << "学生信息修改成功!\n"; return; } p = p->next; } cout << "未找到该学生的信息!\n"; }的运行逻辑
这是一个 C++ 中用链表实现修改学生信息的函数。函数的输入参数是一个指向链表头节点的指针 head,函数的作用是根据输入的学生学号,修改该学生的信息。
函数首先从控制台输入要修改的学生学号 id。然后定义一个指针 p,并将其初始化为链表头节点的 next 指针,即第一个学生信息节点。接下来,使用 while 循环遍历整个链表,查找学号等于 id 的学生信息节点。
如果找到了该学生信息节点,函数从控制台输入修改后的学生信息,包括姓名、年龄、性别和成绩,并将它们更新到该学生信息节点中。最后,输出一条修改成功的提示信息,函数结束执行。
如果遍历整个链表后仍然没有找到学号等于 id 的学生信息节点,则输出一条未找到该学生信息的提示信息。
总之,这个函数的逻辑是:从控制台输入要修改的学生学号,遍历整个链表查找该学生信息节点,如果找到则从控制台输入修改后的学生信息并更新到该学生信息节点中,输出一条修改成功的提示信息;如果未找到则输出一条未找到该学生信息的提示信息。
阅读全文
相关推荐














