显示学生参加的课程和他/她的名字(其中包含一些不需要的字符)时,我得到了一些意外的输出。如何解决此问题?
这是我的完整代码:
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
#define MAX_LEN 5
typedef struct List_of_Students {
string surname;
char group[MAX_LEN];
struct List_of_Students *next_student;
char *name_student;
char* attended_courses;
};
class Student{
List_of_Students* head_Student;
List_of_Students* found_Student=nullptr;
int no_students=0;
public:
Student() {
head_Student = nullptr;
}
void insert(string surname,vector<char> name_student,char *group,vector<char> attended_courses) {
List_of_Students* temp = new List_of_Students;
strcpy(temp->group, group);
temp->surname = surname;
temp->name_student = new char[name_student.size()];
temp->attended_courses = new char[attended_courses.size()];
copy(name_student.begin(), name_student.end(), temp->name_student);
copy(attended_courses.begin(), attended_courses.end(), temp->attended_courses);
temp->next_student = NULL;
if (head_Student == nullptr)
head_Student = temp;
else {
List_of_Students* traversal_pointer;
for (traversal_pointer = head_Student; traversal_pointer->next_student; traversal_pointer = traversal_pointer->next_student);
traversal_pointer->next_student = temp;
}
}
void Set_no_students(int no_decision) {
if (no_decision == 1)
no_students++;
else if (no_decision == 2)
no_students--;
}
bool Search_Student(string surname,int user_choice=0) {
for(List_of_Students* traversal_pointer=head_Student;traversal_pointer;traversal_pointer=traversal_pointer->next_student)
if (traversal_pointer->surname == surname) {
if(user_choice==4) //I'll keep the adreess of this particular pointer,in case I request a certain student
Set_Student(traversal_pointer);
return true;
}
return false;
}
void Pop(string surname) {
List_of_Students* temp;
if (head_Student->surname == surname) {
temp = head_Student;
head_Student = head_Student->next_student;
cout << "Studentul " << temp->surname <<" "<<temp->name_student<<" din grupa " << temp->group << " a fost sters din lista de studenti." << endl; //Message displaying that the student is being removed from the list
delete(temp);
}
else {
List_of_Students* traversal_pointer;
for (traversal_pointer = head_Student; traversal_pointer->next_student; traversal_pointer = traversal_pointer->next_student) {
if (traversal_pointer->next_student->surname == surname) {
temp = traversal_pointer->next_student;
cout << "Studentul cu numele " << temp->surname <<" "<<temp->name_student<<" din grupa " << temp->group << " a fost sters din lista de studenti." << endl;//Message displaying that the student is being removed from the list
traversal_pointer->next_student = traversal_pointer->next_student->next_student;
delete(temp);
break;
}
}
}
}
int Get_no_students() { return no_students; }
void display_List_of_Students() {
for (List_of_Students* traversal_pointer = head_Student; traversal_pointer; traversal_pointer = traversal_pointer->next_student)
cout <<"Nume student:"<<traversal_pointer->surname <<"\tPrenume student:"<<traversal_pointer->name_student<<"\tGrupa:"<<traversal_pointer->group<<"\tCursuri frecventate:"<<endl<<traversal_pointer->attended_courses;//Student Surname/Student Name/Group/Attended Courses
cout << endl;
}
List_of_Students* Get_Student() { return found_Student;}
void Set_Student(List_of_Students* found_student) { found_Student = found_student; }
};
class Curs{
};
int main() {
Student list_Students;
int current_input_character;
int user_choice;
int no_students;
string surname;
vector<char> attended_courses;
vector<char> student_name;
char group[MAX_LEN];
//Curs list_courses;
//cout<<list_courses.Get_no_students();
printf("1.Inserare student in lista.\n2.Stergerea unui student din lista.\n3.Determinarea numarului de studenti.\n4.Cautarea unui student.\n6.Afisare lista studenti.\n");
do {
cin >> user_choice;
switch (user_choice) {
case 1:
list_Students.Set_no_students(user_choice);
printf("Care este numele studentului?"); //Surname of the student
cin >> surname;
cin.ignore();
printf("Care este prenumele studentului?"); //Name of the student
while (true) {
current_input_character = getchar();
if (current_input_character == '\n')
break;
student_name.push_back(current_input_character);
}
cout << student_name.size();
/*for (auto iterator : student_name)
cout << iterator;*/
cout << endl;
printf("In ce grupa face parte:"); //Requesting group of student
cin.getline(group, MAX_LEN);
printf("Ce cursuri frecventeaza?(P.S:Ca sa termini de citit inforatiile,apesi ctrl+Z:)\n");//Requesting attended courses
while ((current_input_character = getchar()) != EOF) {
attended_courses.push_back(current_input_character);
}
cout << attended_courses.size();
list_Students.insert(surname, student_name,group,attended_courses);
/*for (auto iterator : course_name)
cout << iterator;*/
break;
case 2:
printf("Pe care student vrei sa il stergi?");//Which Student I want to remove from the list
cin >> surname;
if (list_Students.Search_Student(surname))
list_Students.Pop(surname);
else
cout<<"Studentul"<<surname<<" fie a fost deja sters din lista sau nu a fost niciodata trecut pe lista de studenti."<<endl;//Message for a possible,already deleted student from list of students.
break;
case 3:
cout << "Numarul de studenti este:" << list_Students.Get_no_students() << endl;
break;
case 4:
printf("Pe care student vrei sa il cauti?"); //Requesting name of a student
cin >> surname;
if (list_Students.Search_Student(surname,user_choice)) {
cout << "Studentul " << list_Students.Get_Student()->surname << " se afla in grupa " << list_Students.Get_Student()->group << endl;//Mentioning which group the student is in
}
else {
cout << "Studentul " << surname << " nu se afla in lista." << endl; //Message for student not being in list
}
break;
case 6:
list_Students.display_List_of_Students();
break;
}
} while (user_choice);
return 0;
}
我现在可以确定的是,当尝试将两个insert
的内容复制到vector<char>
时,char*
函数肯定发生了错误:>
void insert(string surname,vector<char> name_student,char *group,vector<char> attended_courses) {
List_of_Students* temp = new List_of_Students;
strcpy(temp->group, group);
temp->surname = surname;
temp->name_student = new char[name_student.size()];
temp->attended_courses = new char[attended_courses.size()];
copy(name_student.begin(), name_student.end(), temp->name_student);
copy(attended_courses.begin(), attended_courses.end(), temp->attended_courses);
temp->next_student = NULL;
if (head_Student == nullptr)
head_Student = temp;
else {
List_of_Students* traversal_pointer;
for (traversal_pointer = head_Student; traversal_pointer->next_student; traversal_pointer = traversal_pointer->next_student);
traversal_pointer->next_student = temp;
}
}
void Set_no_students(int no_decision) {
if (no_decision == 1)
no_students++;
else if (no_decision == 2)
no_students--;
}
P.S:很抱歉,我在输出中显示的是罗马尼亚语消息,但希望这些注释对您有所帮助。