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.