int stu_count = 0, stu_index=0, i1, i2, choice, end = 0;
struct student student_list[100];
do {
cout<< "\n-----------------------------------------------------------------\n";
cout<< "1: Add a student to the data base\n";
cout<< "2: Add a course to a student in the data base\n";
cout<<"3: Find and remove a student from the data base\n";
cout<<"4: Find and remove a course of a student in the data base\n";
cout<<"5: Print out the courses of a student in the data base\n";
cout<<"6: Print out all the students in the data base\n";
cout<<"7: Print out all students and all their courses in the data base\n\n";
cout<<"Enter 1-7 to select function to preform or enter 0 to quit: ";
if ((cin>>choice) == 0) {
fflush(stdin);
cout<<"Invalid choice.\n";
continue;
}
cout<<"\n";
switch (choice) {
case 0:
end = 1;
break;
case 1:
add_student(student_list,&stu_count);
break;
case 2:
add_course(student_list,&stu_count);
break;
case 3:
remove_student(student_list,&stu_count);
break;
case 4:
remove_course(student_list,&stu_count);
break;
case 5:
print_student_courses(student_list, &stu_count);
break;
case 6:
print_all_students(student_list, &stu_count);
break;
case 7:
print_all_courses_all_students(student_list,&stu_count);
break;
int stu_id,i,stu_index;
char stu_course[7];
struct class1 *p1, *p2;
cout<<"Please enter the ID of the student you want to remove course: ";
cin>>stu_id;
for (i=0; i< *stu_count; i++)
if (stu_id == student_list[i].id) {
stu_index = i;
break;
}
cout<<"Please enter the course of the student you want to remove: ";
cin>>stu_course;
p1 = student_list[stu_index].p_class_from_student;
if(p1 != NULL){
if (strcmp(p1->course_name, stu_course) == 0) {
student_list[stu_index].p_class_from_student = p1->p_class;
delete p1;
}
else {
while (p1 != NULL && strcmp(p1->course_name, stu_course) != 0) {
p2 = p1;
p1 = p1 ->p_class;
}
if(p1 != NULL){ //if a class matched
p2->p_class = p1->p_class;
delete p1;
}else{//no class match
cout<<"Course does not exist.";
}
}
}
}
void print_student_courses(struct student student_list[],int *stu_count)
{
char course_name[7];
int temp=0;
int stu_id, i, stu_index;
struct class1 *p1;
cout<<"Please enter student ID to print courses: ";
cin>>stu_id;
cout<<"\nThe student with ID "<<stu_id<< " takes the following courses: \n\n";
cout<<"Course Name Section No. No. of Credit(s) \n";
int i;
cout<<"\nThe IDs of students in the data base are listed below. \n\n";
for ( int pass = 0; pass < *stu_count - 1 ; ++pass )
for ( int j = 0; j < *stu_count - 1 - pass; ++j )
{
//Perform string compare and return value store as result
int result = strcmp (student_list[j], student_list[j+1]);
//If value is less than 0 then perform swap function to rearrange
if (result > 0)
swap ( student_list[j] , student_list[j+1] );
}//for
if (student_list[i].id != -1)
cout<<student_list[i].id<<"\n";//cout<< "Student "<<stu_id<< "does not exist.";
}
void print_all_courses_all_students(struct student student_list[],int *stu_count)
{
int i;
struct class1 *p1;
for (i=0; i<*stu_count; i++)
if (student_list[i].id != -1) {
cout<<"\nThe course(s) of student with ID %d: \n\n", student_list[i].id;
cout<<"Course Name Section No. No. of Credit(s) \n";
p1 = student_list[i].p_class_from_student;
while (p1 != NULL) {
cout << p1->course_name << " " << p1->section << " " << p1->credit<<"\n";
p1 = p1->p_class;
}
}
}