Nested Linked list type pointers initialization problem

I am having problem with initializing the linked list (*exam and *subject) through the constructor of Student class, i tried by creating two new linked lists (sub and exm) and assign their addresses to both pointers (subject and exam)(refer to student class constructor), however i had a problem where the both linked lists contains garbage values such as count in exam linked list = -853218796854 instead of getting 0 the codes is given below... Each classes in different header file.

Student class:
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
  class Student {		
public:

        char name[30];	
        char id[10];
	char course[3];
	char phone_no[10];
	double current_cgpa; 
	int currentCreditHours;  
	int totalCreditsEarned; 
	DLL<Exam>	*exam;
	DLL<Subject> *subject;

	Student();
	
};

Student::Student(){
	
	//To set name to empty
	for(int count = 0; count < 30; count++){
		name[count] = ' ';
	}
	//To set course to empty
	for(int count2 = 0; count2 < 3; count2++){
		course[count2] = ' ';
	}
	//To set phone number to empty
	for(int count3 = 0; count3 < 10; count3++){
		phone_no[count3] = ' ';
	}

	id[0] = 0;
	id[1] = '\0';
	current_cgpa = 0;
	totalCreditsEarned = 0;
	currentCreditHours = 0;

	DLL<Subject> sub;
	DLL<Exam> exm;
	subject = &sub;
	exam = &exm;
	
}


Subject class:
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
class Subject{

public:
	char subject_code[8];
	char subject_name[256];
	int credit_hours;
	double marks;
	double attendance;
	double fees;
	bool payment;
	Subject();
	
	
};

Subject::Subject(){
	
	//To set subject code to empty
	for(int count = 0; count < 8; count++){
		subject_code[count] = ' ';
	}
	//To set subject name to empty
	for(int count2 = 0; count2 < 256; count2++){
		subject_name[count2] = ' ';
	}

	credit_hours = 0;
	marks = 0;
	attendance = 0;
	fees = 0;
	payment = false;
}


Exam class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Exam {		
public:

	int trimester; //1 = jan, 5 = may and 10 = oct
	int year;
	int totalcredit;
	double gpa;
	int numOfSubjects; //for one trimester
	Subject sub[6]; //list of subjects taken by student in a semester
	Exam();
	

};

Exam::Exam(){
	trimester = 0;
	year = 0;
	gpa = 0.0;
	numOfSubjects = 0;
	totalcredit = 0;
}


DLLNode class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <class T>
class DLLNode {
	public:
		T		item;
		DLLNode<T>	*next;
		DLLNode<T> *prev;
		DLLNode(T);
};

template <class T>
DLLNode<T>::DLLNode(T newItem) {
	item = newItem;
	next = NULL;
	prev = NULL;
}


DLL class
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
template <class T>
class DLL {
	private:
		int		count;
		DLLNode<T>	*head;
		DLLNode<T>	*find(int);
	public:
		DLL();
		~DLL();
		
};

template <class T>
DLL<T>::DLL() {
	head = NULL;
	count = 0;
}

template <class T>
DLL<T>::~DLL() {

	for(int i=1; i<=size(); i++)
		remove(1);

	head = NULL;
}
Last edited on
You are assigning your exam and subject pointers to local variables whose scope expires at the end of the constructor. You need to use new to create a new instance for subject and exam.

Put the following into your constructor:
1
2
subject = new DLL<Subject>;
exam = new DLL<Exam>;


And remove the sub and exm vars you are using now.
oh nice now it works , thanks, been working this thing for straight 12 hours tried your method before it has some syntax error ,was wondering why and stop using that, guess i type something wrong LOL , thanks again man!
after it works, i take a rest, after that now i check again i had another problem, for all students with exam linked list are point to same memory address, so when i set new updated student with added exam records for a particular student, all other students also get the exam records, i apologize for my English, maybe its hard to understand.

DLL<Student> aStudent;

inside first node of aStudent:
exam with memory address 0x00385130

inside second node of aStudent:
exam with memory address 0x00385130

..... all exam nodes in aStudent will have the same record , so what should i do to avoid that?
..... all exam nodes in aStudent will have the same record , so what should i do to avoid that?

Chances are something is off in the code that is responsible for inserting data into the list.
Any example?
Any example?

You want me to give you an example using the code you haven't supplied? My crystal ball is out for repairs.
I actually tried, Student class contains 2 more sub linked list , which are exam and subject, for this piece of code i only add exam into student, and all other student can access to a particular student exam in linked list , hope you can understand.

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
bool InsertResult(char *filename, DLL<Student> *dlist){
	ifstream filetxt;
	Student tmp;
	Exam atmp;
	filetxt.open(filename);
	if(!filetxt){
		cout<<"Can not open the file"<<endl;
		return false;
	}

	while(!filetxt.eof()){
		
		Student aStudent,tmpstudent;
		Exam aExam;
		double cgpa;
		//char id[10];

		filetxt>>tmpstudent.id;
		for(int i =1;i<=dlist->size();i++){
			dlist->get(i,aStudent);
			if(strcmp(aStudent.id,tmpstudent.id)==0){
				
				filetxt>>aExam.trimester;
				filetxt>>aExam.year;
				filetxt>>aExam.numOfSubjects;

				for(int x = 0;x<aExam.numOfSubjects;x++){
					filetxt>>aExam.sub[x].subject_code;
					filetxt>>aExam.sub[x].subject_name;
					filetxt>>aExam.sub[x].credit_hours;
					filetxt>>aExam.sub[x].marks;
				}
				
				aExam.calculateGPA();
				aStudent.exam->insert(aExam);
				dlist->set(i,aStudent);
			
			}

			
		}
		
	}
	
	return true;
}
Topic archived. No new replies allowed.