Hi, so I have a linked list with nodes storing student names, course, id, phone number and so on. In the function below, i am trying to read from a file and add exam information to the existing nodes according to student id.
For context, In my Node, I stored
, which contains student id, name, course, phone number, currentcgpa, exam_cnt and an exam struct array.
Within the exam struct array, im storing trimester, year, numofsubjects and Subject sub; struct array.
Within the sub struct array, im storing subject code, name, credit hours and marks
From what i have done, the exam information is stored correctly according to student id, but the problem is the student name, course and phone number of my last node
replaced all the other nodes (the student id and exam results were not replaced)
eg. Student 1 : 1200233 Tom Hills CS 790-3223
//some exam struct that belonged to the student with stu.id 1200233
Student2: 1201237 Tom Hills CS 790-3223
//some exam struct that belonged to the student with stu.id 1201237 |
(Tom Hills CS 790-3223 was stored in the last node of my linked list)
Can someone tell me what I have done wrong in my code below? Because Ive looked over it again and again and I thought it was supposed to working fine
exam.txt contents:
1201237 5 2012 2 UCCD1024 DATA_STRUCTURES_AND_ALGORITHMIC_PROBLEM_SOLVING 4 80 UCCD2502 INTRODUCTION_TO_INVENTIVE_PROBLEM_SOLVING_AND_PROPOSAL_WRITING 3 78
1300899 10 2012 2 UCCD1004 PROGRAMMING_CONCEPTS_AND_DESIGN 4 70 UCCD1024 DATA_STRUCTURES_AND_ALGORITHMIC_PROBLEM_SOLVING 4 80
1200233 5 2012 1 UCCD2502 INTRODUCTION_TO_INVENTIVE_PROBLEM_SOLVING_AND_PROPOSAL_WRITING 3 70
1541359 5 2015 2 UCCD3248 INTRODUCTION_TO_FASHION 4 90 UCCN7894 COLOUR_PSYCHOLOGY 3 75
1687432 1 2016 3 UCCD1004 PROGRAMMING_CONCEPTS_AND_DESIGN 4 38 UCCD1024 DATA_STRUCTURES_AND_ALGORITHMIC_PROBLEM_SOLVING 4 60 UCCD2502 INTRODUCTION_TO_INVENTIVE_PROBLEM_SOLVING_AND_PROPOSAL_WRITING 3 52
2162486 5 2021 2 UCCD2502 INTRODUCTION_TO_INVENTIVE_PROBLEM_SOLVING_AND_PROPOSAL_WRITING 3 85 UCCS1234 INTRODUCTION_TO_THEATRE 4 86
1900648 10 2020 1 UCCD1024 DATA_STRUCTURES_AND_ALGORITHMIC_PROBLEM_SOLVING 4 52
1200233 1 2013 3 UCCD1004 PROGRAMMING_CONCEPTS_AND_DESIGN 4 70 UCCD1024 DATA_STRUCTURES_AND_ALGORITHMIC_PROBLEM_SOLVING 4 80 UCCD2502 INTRODUCTION_TO_INVENTIVE_PROBLEM_SOLVING_AND_PROPOSAL_WRITING 3 78 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
bool List::set(int position, type newItem) {
if (position > count) return false;
find(position)->item = newItem;
return true;
} //this is in another cpp file
[\code]
[code]
bool InsertExamResult(char *filename, List *list)
{
ifstream e;
e.open("exam.txt");
if (!e)
{
cout << "Could not open exam.txt.\n";
return false;
}
else
{
while (e >> stu.id)
{
Node *current = list->head;
int position = 1;
while (current != NULL)
{
if (strcmp(current->item.id, stu.id) == 0) //traverse list and find the node with matching student id to insert
{
e >> stu.exam->trimester >> stu.exam->year >> stu.exam->numOfSubjects;
cout << stu.id << stu.exam->trimester << stu.exam->year << stu.exam->numOfSubjects << endl; //to check if read correctly
stu.exam_cnt = current->item.exam_cnt;
for (int j = 0; j < stu.exam[stu.exam_cnt]->numOfSubjects; j++)
{
e >> stu.exam[stu.exam_cnt].sub[j].subject_code >> stu.exam[stu.exam_cnt].sub[j].subject_name >> stu.exam[stu.exam_cnt].sub[j].credit_hours >> stu.exam[stu.exam_cnt].sub[j].marks;
cout << stu.exam[stu.exam_cnt].sub[j].subject_code << stu.exam[stu.exam_cnt].sub[j].subject_name << stu.exam[stu.exam_cnt].sub[j].credit_hours << stu.exam[stu.exam_cnt].sub[j].marks << endl;
}
stu.exam[stu.exam_cnt].calculateGPA(); //this is a funtion in exam struct array
cout << stu.exam[stu.exam_cnt].gpa << endl;
stu.exam_cnt++;
cout << stu.exam_cnt << endl;
stu.calculateCurrentCGPA(); //this is a function in student struct
cout << stu.current_cgpa << endl;
list->set(position, stu); //did i use this set function correctly?
}
current = current->next;
position++;
}
}
e.close();
return true;
}
}
|