Hi All,

I am working on a project and have come across a slight problem. I have a file i am reading in that consists of the following:

John Smith 333222333 01/01/80 M z11119
cs111 2008 fall a
cs222 2009 spring f

The first line is the Students information, the second part is the classes this student has taken, when they took it and the grade received. I am created a linked list to store the classes ( i had previously done with this an array, but a linked list gives me the ability to add unlimited classes etc).

So, here is where my problem occurs. I have the program read in from the above file, then set the students name (first line data) and also set the class info or each class. At the end of the while loop that reads as many classes as needed, i have a function call that should add the object into the linked list.

here is that function that reads in from file:

void Student::loadInfoFromFile(fstream& inFile)
{
	string firstName, lastName, SSN, DOB, gender, 
		   ZID, courseID, year, semester, grade;

	inFile >> firstName;
	Person::setFirstName(firstName);
	inFile >> lastName;
	Person::setLastName(lastName);
	inFile >> SSN;
	Person::setSSN(SSN);
	inFile >> DOB;
	Person::setDOB(DOB);
	inFile >> gender;
	Person::setGender(gender);
	inFile >> ZID;
	setZID(ZID);

	//start class read
	inFile >> courseID;
	
	while (inFile)
	{
	inFile >> year;
	inFile >> semester;
	inFile >> grade;
	cout << "Course ID: " << courseID << endl;
	cout << "Year: " << year << endl;
	cout << "Semester: " << semester << endl;
	cout << "Grade: " << grade << endl;
	 
	CourseInfo courses;
	courses.setCourseID(courseID);
	courses.setYearTaken(year);
	courses.setSemesterTaken(semester);
	courses.setGrade(grade);

	courses.addCourse(getZID(), courses);
	inFile >> courseID;
	}	
  }

as you see i am passing the ZID and the courses object to the addCourse() function. This function simply calls another function, addToHead() that will add the object to the linked list. I am having trouble with the implementation of this. My addcourse() is this:

void CourseInfo::addCourse(string ZID, CourseInfo c1)
{
	cout << "IN ADD COURSE!!!" << endl;
//	addTohead(c1); 
}

I cannot figure out how to call the function addToHead(). I am sure i have to do SOMETHING.addToHead(c1); but am unsure what. Having tried multiple variances, i still have no clue.

My addToHead() looks like:

template <class T>
void SinglyLinkedList<T>::addToHead(T c1)
{
	head = new nodeSLL<T>(cl, head);
	if (tail == NULL)
		tail = head;
}

So it should create a new node, calling the constructor with 2 passed in arguments. Should i have this constructor do something like:

template <class T>
nodeSLL(CourseInfo val, nodeSLL<T> *head) 
{
   info = val; 
   next = head;
}

Would this work? Does this look ALMOST correct? Any input as to how to implement this better, please leave me a note. I will post all the code i have written to this post.

Thank you in advance.

Recommended Answers

All 10 Replies

Add a linked list object to your course info class. Then in the course info constructor create the linked list object. Then use that linked list when adding courses in the addCourse function.

I have a SinglyLinkedList object in my Student.h file. Is this the sort of thing you are talking about? I have that there and mention it when doing my readinfromfile() function within the student.h file. I use that to set the values such as:

CourseInfo courses;
	courses.setCourseID(courseID);
	courses.setYearTaken(year);
	courses.setSemesterTaken(semester);
	courses.setGrade(grade);

Sorry if this seems rather stupid of me, but i am obviously NOT an expert in c++ and need a little more hand holding sadly.

Thank you for your patience though, it is really really appreciated!

Yes, since your linked list is public in the student class you can access it by using a student object for example:

student s;
s.courseList.addToHead(courseInfo);

There is a problem with this logic though. The way in which i am told to set this up, i have to call addCourse from the readinfromfile() function within the courseInfo.h file. This addCourse() calls addToHead() which is in the singlylinkedlist.h file. The addCourse() function gets passed the student ID number and the CourseInfo object created within the readinfromfile() function.

Therefore, to call the addTohead() function i would need some way to reference the function when i do not have a linkedlistobject available to me correct? I have the CourseInfo object which is passed in here as c1 here:

void CourseInfo::addCourse(string ZID, CourseInfo c1)
{
	cout << "IN ADD COURSE!!!" << endl;
	WHATHERE.courseList.addTohead(c1);
}

Not sure where to put as the WHATHERE part. I feel like a right idiot here. I am completely lost and confused. Ugh!

You would need the addCourse function to accept a student as a parameter. For example:

void CourseInfo::addCourse(string ZID, CourseInfo c1, student s)
{
	cout << "IN ADD COURSE!!!" << endl;
	s.courseList.addTohead(c1);
}

Originally i thought that would be simple. I just attempted that and here is what i did:
CourseInfo.h

void addCourse(string, CourseInfo, Student);

CourseInfo.cpp

void CourseInfo::addCourse(string ZID, CourseInfo c1, Student s)
{
	cout << "IN ADD COURSE!!!" << endl;
	s.courseList.addTohead(c1);
}

Driver.cpp

s1.loadInfoFromFile(inFile, s1);

Student.h

void loadInfoFromFile(fstream& inFile);

Student.cpp

void Student::loadInfoFromFile(fstream& inFile, Student s)
{
...
...
	courses.addCourse(getZID(), courses, s);
	inFile >> courseID;
	}

And i get 9 errors here - simply because i do not have the ability to use a Student object within the CourseInfo class. So i am now completely unsure how i can make that work! Any more ideas? Thank you once again.

post the errors you are getting

courseinfo.h(28) : error C2061: syntax error : identifier 'Student'
courseinfo.cpp(137) : error C2061: syntax error : identifier 'Student'
courseinfo.cpp(140) : error C2065: 's' : undeclared identifier
courseinfo.cpp(140) : error C2228: left of '.courseList' must have class/struct/union
type is ''unknown-type''
courseinfo.cpp(140) : error C2228: left of '.addTohead' must have class/struct/union
1>driver.cpp
courseinfo.h(28) : error C2061: syntax error : identifier 'Student'
driver.cpp(57) : error C2660: 'Student::loadInfoFromFile' : function does not take 2 arguments
Student.cpp
courseinfo.h(28) : error C2061: syntax error : identifier 'Student'
student.cpp(104) : error C2511: 'void Student::loadInfoFromFile(std::fstream &,Student)' : overloaded member function not found in 'Student'
student.h(17) : see declaration of 'Student'

Thanks once again for your time!

Okay, we will try to solve this 1 error at a time. One error is that it doesn't know what a Student is in the courseinfo class because you haven't included the definition of it. Add #include "Student.h" to your courseinfo.h

After you fix that, post the new errors you get.

Thanks for your help so far. I have got emails from my TA that informed me of a certain thing i was stuck on, and i also restarted my project using a slightly different drive program. I have got to the point in which i belive i should be able to print out the list of data for a student from the courseList object i have created. My problem right now is that for whatever reason, i can only get one class to print, even though i know it has read in more than one.

The class that gets printed is the last class to be read in. So i think that my pointers are being set incorrectly. But i cannot figure out where the mistake is. if you could see the code and compile a version - you would easily see what i mean. I am pretty sure my 'head' pointer within the singlylinked class is being set incorrectly. If this is the case, how should i be setting it...

1) the way in which i create a new node and store the courseList object is incorrect.
2) i do the creating new node and setting info to the courseList object passed it, BUT i incorrectly set the links, so my link is effectively broken.
3) i have done it all wrong and should just give up!

So, please could you take a look at the code, give it a run, and see where you think i have made a mistake in my link/creation of my node/or the storing of my courseList object.

Thanks a ton for all your support

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.