本文介绍了我正在尝试使用单个链接列表阅读学生和教师的详细信息。编译器在屏幕上没有显示任何错误。我可以修复此问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在尝试从不同的文件中读取学生和教师的详细信息,并使用来自学生和教师的两个不同的课程。当我编译我的程序时,它没有显示任何错误,但我把break放在main中然后运行它。在条件断点停止时首先读取文件 i am trying to read student and teacher details from different files and using two different classes from both student and teacher.When i compile my program it doesn't show any error but i put break in main and then run it. while reading file in first while condition break point stops at S.create_node(name, course_code, marks, cgpa); 任何想法我怎么能解决这个问题,因为我对这个编程概念很陌生。 我尝试了什么: any idea how can i fix this problem since i am very new to this programming concept.What I have tried:#include<iostream>#include<fstream>#include<stdio.h>#include<string>using namespace std;struct Node{ string s_name; string s_course_code; int s_marks; float s_cgpa; string t_name; int t_age; int t_Class; string t_subject; Node *next;};class student{private: Node *head;public: student(); void create_node(string name, string course_code, int marks, float cgpa); void delete_node(int node_no); void display(); void search_name(string name);};student::student(){ head = NULL;}void student::create_node(string name, string course_code, int marks, float cgpa){ int size = 0; Node *temp = new Node; temp->s_name = name; temp->s_course_code = course_code; temp->s_marks = marks; temp->s_cgpa = cgpa; temp->next = NULL; if (!head) { head = temp; } else { Node *t = head; while (t->next != NULL) { t = t->next; } temp = t; t->next = temp; t = t->next; } size++;}void student::delete_node(int node_no){ int counter = 0; // Check if node is exist if (node_no > counter) { cout << "No such node is exist"; } else { Node *temp1; // create a temporary node temp1 = (Node*)malloc(sizeof(Node)); // allocate space for node temp1 = head; // transfer the address of 'head' to 'temp1' Node *old_temp; // create a temporary node old_temp = (Node*)malloc(sizeof(Node)); // allocate space for node old_temp = temp1; // transfer the address of 'temp1' to 'old_temp' // Check node number is 1 if (node_no == 1) { head = temp1->next; // transfer the address of 'temp1->next' to 'head' free(temp1); counter--; cout << node_no << " node of the Linked List is deleted" << endl; } else { // Go to the node number of the node for (int i = 1; i < node_no; i++) { old_temp = temp1; // store previous node temp1 = temp1->next; // store current node } old_temp->next = temp1->next; // transfer the address of 'temp1->next' to 'old_temp->next' free(temp1); counter--; cout << node_no << " node of the Linked List is deleted" << endl; } }}void student::display(){ if (!head) { cout << "File is Empty.........................." << endl; } else { Node *t = head; //while(t->next!=NULL) //t=t->next; int count = 1; cout << endl << endl; while (t != NULL) { cout << "[" << count << "]=>" << endl; cout << "\t\tName:" << t->s_name << endl; cout << "\t\tCourse Code:" << t->s_course_code << endl; cout << "\t\tMarks:" << t->s_marks << endl; cout << "\t\tCGPA:" << t->s_cgpa << endl; count++; t = t->next; } }}void student::search_name(string name){ if (!head) { cout << "File is Empty.........................." << endl; } else { Node *t = head; //while(t->next!=NULL) //t=t->next; int count = 1; cout << endl << endl; while (t != NULL) { if (t->s_name == name) { cout << "[" << count << "]=>" << endl; cout << "\t\tName:" << t->s_name << endl; cout << "\t\tCourse Code:" << t->s_course_code << endl; cout << "\t\tMarks:" << t->s_marks << endl; cout << "\t\tCGPA:" << t->s_cgpa << endl; } count++; t = t->next; } }}class teacher{private: Node *head;public: teacher(); void create_node(string name, string subject, int Class, int age); void delete_node(int node_no); void display(); void search_name(string name);};teacher::teacher(){ head = NULL;}void teacher::create_node(string name, string subject, int Class, int age){ int size = 0; Node *temp = new Node; temp->t_name = name; temp->t_subject = subject; temp->t_Class = Class; temp->t_age = age; temp->next = NULL; if (!head) { head = temp; } else { Node *t = head; while (t->next != NULL) { t = t->next; } temp = t; t->next = temp; t = t->next; } size++;}void teacher::delete_node(int node_no){ int counter = 0; // Check if node is exist if (node_no > counter) { cout << "No such node is exist"; } else { Node *temp1; // create a temporary node temp1 = (Node*)malloc(sizeof(Node)); // allocate space for node temp1 = head; // transfer the address of 'head' to 'temp1' Node *old_temp; // create a temporary node old_temp = (Node*)malloc(sizeof(Node)); // allocate space for node old_temp = temp1; // transfer the address of 'temp1' to 'old_temp' // Check node number is 1 if (node_no == 1) { head = temp1->next; // transfer the address of 'temp1->next' to 'head' free(temp1); counter--; cout << node_no << " node of the Linked List is deleted" << endl; } else { // Go to the node number of the node for (int i = 1; i < node_no; i++) { old_temp = temp1; // store previous node temp1 = temp1->next; // store current node } old_temp->next = temp1->next; // transfer the address of 'temp1->next' to 'old_temp->next' free(temp1); counter--; cout << node_no << " node of the Linked List is deleted" << endl; } }}void teacher::display(){ if (!head) { cout << "File is Empty.........................." << endl; } else { Node *t = head; //while(t->next!=NULL) //t=t->next; int count = 1; cout << endl << endl; while (t != NULL) { cout << "[" << count << "]=>" << endl; cout << "\t\tName:" << t->t_name << endl; cout << "\t\tSubject" << t->t_subject << endl; cout << "\t\tClass" << t->t_Class << endl; cout << "\t\tAge:" << t->t_age << endl; count++; t = t->next; } }}void teacher::search_name(string name){ if (!head) { cout << "File is Empty.........................." << endl; } else { Node *t = head; //while(t->next!=NULL) //t=t->next; int count = 1; cout << endl << endl; while (t != NULL) { if (t->t_name == name) { cout << "[" << count << "]=>" << endl; cout << "\t\tName:" << t->t_name << endl; cout << "\t\tSubject:" << t->t_subject << endl; cout << "\t\tClass" << t->t_Class << endl; cout << "\t\tAge:" << t->t_age << endl; } count++; t = t->next; } }}int main(){ student S; teacher T; string T_name; string subject; int age; int Class_teacher; string course_code; int marks; float cgpa; string name; int ch; ifstream fin,fin1,fin2,fin3,fin4, fin5, fin6, fin7; fin.open("s_name.txt"); fin1.open("s_course.txt"); fin2.open("s_marks.txt"); fin3.open("s_cgpa.txt"); fin4.open("t_name.txt"); fin5.open("t_subject.txt"); fin6.open("t_class.txt"); fin7.open("t_age.txt"); while (!fin.eof() && !fin1.eof() && !fin2.eof() && !fin3.eof() && !fin4.eof() && !fin5.eof() && !fin6.eof() && !fin7.eof()) { fin >> name; fin1 >> course_code; fin2 >> marks; fin3 >> cgpa; fin4 >> T_name; fin5 >> subject; fin6 >> Class_teacher; fin7 >> age; S.create_node(name, course_code, marks, cgpa); T.create_node(T_name, subject, Class_teacher, age); } while (1) { cout << "-------------------STUDENT MANAGEMENT FUNCTION----------------------------" << endl; cout << "Enter 1 to Create_Student_Record:" << endl; cout << "Enter 2 to Display the Student_Record:" << endl; cout << "Enter 3 to Find the Student with Name:" << endl; cout << "Enter 4 to Delete the Record of the Student:" << endl; cout << "-------------------TEACHER MANAGEMENT FUNCTION----------------------------" << endl; cout << "Enter 5 to Create_Teacher_Record:" << endl; cout << "Enter 6 to Display the Teacher_Record:" << endl; cout << "Enter 7 to Find the Teacher with Name:" << endl; cout << "Enter 8 to Delete the Record of the Teacher:" << endl; cout << "Enter 9 for Exit:" << endl; cin >> ch; if (ch == 1) { cout << "Enter the Name:" << endl; cin >> name; cout << "Enter the Course Code:" << endl; cin >> course_code; cout << "Enter the marks:" << endl; cin >> marks; cout << "Enter the CGPA:" << endl; cin >> cgpa; S.create_node(name, course_code, marks, cgpa); } else if (ch == 2) { S.display(); } else if (ch == 3) { string x; cout << "Enter the name:" << endl; cin >> x; S.search_name(x); } else if (ch == 4) { int x; cout << "Enter the Student Location-Number:" << endl; cin >> x; S.delete_node(x); cout << "Student at Location " << x << " is deleted" << endl; } else if (ch == 5) { cout << "Enter the Name:" << endl; cin >> T_name; cout << "Enter the subject:" << endl; cin >> subject; cout << "Enter the class:" << endl; cin >> Class_teacher; cout << "Enter the age" << endl; cin >> age; T.create_node(T_name, subject, Class_teacher, age); } else if (ch == 6) { T.display(); } else if (ch == 7) { string x; cout << "Enter the name:" << endl; cin >> x; T.search_name(x); } else if (ch == 8) { int x; cout << "Enter the Teacher Location-Number:" << endl; cin >> x; T.delete_node(x); cout << "Teacher at Location " << x << " is deleted" << endl; } else if (ch == 9) { exit(1); } else { cout << "Enter the valid Input......" << endl; } system("pause"); system("cls"); } return 0;} 推荐答案 这就是断点所在 - 停止程序执行所以你可以看看发生了什么,并通过你的代码来查找问题。 你如何使用调试器取决于你的编译器系统,但快速谷歌的名字您的IDE和调试器应该为您提供所需的信息。 在函数的第一行放置一个断点,并通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。 对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!That's what a break point is there for - to stop your program executing so you can look at what is going on, and step though your code to find problems.How you use the debugger depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging! 问题在于此代码部分: The problem is in this code part:temp = t;t->next = temp;t = t->next;当执行到达该代码时, t 是最后一个节点。通过将 temp 分配给 t ,它实际上与 When execution reaches that code, t is the last node. By assigning temp to t, it is effectively the same ast->next = t;在下次搜索最后一个节点时会产生一个endloss循环。解决方案是从上面的块中删除第一个和最后一个(无用的)语句。 但是代码中存在更多问题(我没有检查所有代码): 您正在使用C分配( malloc )混合C ++内存分配( new )。从不这样做。 你有 count [er] 和 size 函数中的局部变量。如果你打算在全球范围内使用它们,你必须让它们静态或(更好)成为班级成员。 本地(见上文)计数器在 delete_node()函数中的变量初始化为零,以便函数始终返回传递正 node_no 。 which results in an endloss loop when searching for the last node the next time. The solution is to remove the first and last (useless) statement from the above block.But there are more problems in your code (I did not checked all the code):You are mixing C++ memory allocation (new) with C allocations (malloc). Never do such.You have count[er] and size local variables in your functions. If you intend to use those globally you have to make them static or (better) being class members.The local (see above) counter variable in your delete_node() function is initialised with zero so that the functions always returns when passing a positive node_no. 这篇关于我正在尝试使用单个链接列表阅读学生和教师的详细信息。编译器在屏幕上没有显示任何错误。我可以修复此问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 20:21