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.

Attachments
#ifndef H_340
#define H_340

#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>

#include <cstdlib>
#include <climits>
#include <cmath>
#include <ctime>

#include <algorithm>
#include <functional>
#include <iterator>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>

#include <stdexcept>
using namespace std;
#endif
#include "courseInfo.h"

/****************************************************************
   FUNCTION:   courseInfo()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the constructor for the CourseInfo class
****************************************************************/
CourseInfo::CourseInfo()
{
   count = 0;
}

/****************************************************************
   FUNCTION:   ~CourseInfo()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the destructor for the CourseInfo class
****************************************************************/
CourseInfo::~CourseInfo()
{
}

/****************************************************************
   FUNCTION:   getCourseID() const
   ARGUMENTS:  none
   RETURNS:    string courseID
   NOTES:      this function returns the courseID for the student
****************************************************************/
string CourseInfo::getCourseID() const
{
	return courseID;
}

/****************************************************************
   FUNCTION:   setCourseID(const string &x)
   ARGUMENTS:  const string &x
   RETURNS:    none
   NOTES:      this function sets the courseID for the student
****************************************************************/
void CourseInfo::setCourseID(string &x)
{
	courseID = x;
}

/****************************************************************
   FUNCTION:   setYearTaken(const string &x)
   ARGUMENTS:  const string &x - the new YearTaken
   RETURNS:    none
   NOTES:      this function sets the students YearTaken for a course
****************************************************************/
void CourseInfo::setYearTaken(string &x)
{
	yearTaken = x;
}

/****************************************************************
   FUNCTION:   getYearTaken() const
   ARGUMENTS:  none
   RETURNS:    string yearTaken
   NOTES:      this returns the students YearTaken for a certain class
****************************************************************/
string CourseInfo::getYearTaken() const
{
	return yearTaken;
}

/****************************************************************
   FUNCTION:   setSemesterTaken(const string &x)
   ARGUMENTS:  const string &x - the semesterTaken value
   RETURNS:    none
   NOTES:      this function sets the students semester taken for
               a specific course
****************************************************************/
void CourseInfo::setSemesterTaken(string &x)
{
	semesterTaken = x;
}

/****************************************************************
   FUNCTION:   getSemesterTaken() const
   ARGUMENTS:  none
   RETURNS:    string semesterTaken - returns the semester taken
   NOTES:      this function gets the students semester taken
****************************************************************/
string CourseInfo::getSemesterTaken() const
{
	return semesterTaken;
}

/****************************************************************
   FUNCTION:   setGrade(const string &x)
   ARGUMENTS:  const string &x
   RETURNS:    none
   NOTES:      this functions sets the grade for a certain class
****************************************************************/     
void CourseInfo::setGrade(string &x)
{
	grade = x;
}
      
/****************************************************************
   FUNCTION:   getGrade() const
   ARGUMENTS:  none
   RETURNS:    string grade
   NOTES:      this function returns the grade of the student in 
                a specific class
****************************************************************/
string CourseInfo::getGrade() const
{
	return grade;
}

/****************************************************************
   FUNCTION:   incrementCount(int &x)
   ARGUMENTS:  int x - the value of count
   RETURNS:    none
   NOTES:      this function increments the number of courses input
****************************************************************/
void CourseInfo::incrementCount(int &x)
{
	x = x + 1;
}

/****************************************************************
   FUNCTION:   getCoount() const
   ARGUMENTS:  none
   RETURNS:    int count
   NOTES:      this function returns the count of classes
****************************************************************/
int CourseInfo::getCount() const
{
	return count;
}

void CourseInfo::addCourse(string ZID, CourseInfo c1)
{
	cout << "IN ADD COURSE!!!" << endl;
//	addTohead(c1);
}
/****************************************************************
   FILE:      courseInfo.h
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/23/2009

   PURPOSE:   Includes the class courseInfo information / prototypes 
****************************************************************/
#ifndef COURSEINFO_H
#define COURSEINFO_H

#include "340.h"

class CourseInfo  {
	public:
      CourseInfo();
      virtual ~CourseInfo();
      void setCourseID(string &x);
      string getCourseID() const;
      void setYearTaken(string &x);
      string getYearTaken() const;
      void setSemesterTaken(string &x);
      string getSemesterTaken() const;
      void setGrade(string &x);
      string getGrade() const;
      void incrementCount(int &x);
      int getCount() const;
	  void addCourse(string, CourseInfo);
      
    private:
      string courseID;
      string yearTaken;
      string semesterTaken;
      string grade;
      int count;
      
};


#endif //COURSEINFO_H
/****************************************************************
   PROGRAM:   Project 1
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/26/2009

   FUNCTION:  creates a program that enables a user to input
   		    student data. Including name, zid, SSN, and courses.

   INPUT:     from keyboard

   OUTPUT:    A list of all courses taken/schedules for a certain
   			  student.

   NOTES:     Uses 6 files. Student.h, Student.cpp, Person.h, Person.cpp,
   			  courseInfo.h, courseInfo.cpp, and 340.h.
****************************************************************/
#include "Person.h"
#include "CourseInfo.h"
#include "Student.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
//create Student object and declare variables to be used in creating student
Student s1;
bool empty;
string file;
fstream inFile;

//test if list is empty before data is filled

cout << endl << "Test 1, Check if list is empty:" << endl;
empty = s1.courseList.isEmpty();
cout << "Empty = " << empty;
if(empty == true)
  cout << "Student Object is empty" << endl;
else
  cout << "Student Object is not empty"  << endl;

//read data from file and fill the list with courses

inFile.open("C:\\Documents and Settings\\Tony\\My Documents\\Visual Studio 2008\\Projects\\proj1\\file.txt");

if(!inFile)
  {
  cout << "Can't open file , program terminated." << endl;
  return 0;
  }

s1.loadInfoFromFile(inFile);
inFile.close();


cout << endl << "Test 2, Check if list was filled:" << endl;
s1.infoPrint();

//check if list is empty after data has been filled

cout << endl << "Test 3, Check if list is empty:" << endl;
empty = s1.courseList.isEmpty();
if(empty == true)
  cout << "Student Object is empty" << endl;
else
  cout << "Student Object is not empty"  << endl;
/*
//check to see if you can successfully add a course to the list
cout << endl << "***  ADD A COURSE TO THE STUDENT  ***" << endl << endl;
CourseInfo c1;
string id1, yt1, st1, so1;

cout << "Enter course id to add: ";
cin >> id1;
c1.setCourseId(id1);

cout << "Enter year the course was taken: ";
cin >> yt1;
c1.setYearToTake(yt1);

cout << "Enter semester the course was taken: ";
cin >> st1;
c1.setSemesterToTake(st1);

cout << "Enter grade earned in the course: ";
cin >> so1;
c1.setScoreObtained(so1);

s1.addCourse(s1.getZid(), c1);

cout << endl << "Test 4, check if course was added:" << endl;
s1.infoPrint();

//check to see if you can remove a course from the list
cout << endl << "***  REMOVE A COURSE FROM A STUDENT ***" << endl << endl;

CourseInfo c2;
string id2, yt2, st2, so2;

cout << "Enter course id to remove: ";
cin >> id2;
c2.setCourseId(id2);

cout << "Enter year the course was taken: ";
cin >> yt2;
c2.setYearToTake(yt2);

cout << "Enter semester the course was taken: ";
cin >> st2;
c2.setSemesterToTake(st2);

s1.deleteCourse(s1.getZid(), c2);

cout << endl << "Test 5, check if course was removed:" << endl;
s1.infoPrint();

//check if a certain course exists in the list
cout << endl << "***  CHECK IF A STUDENT HAS TAKEN A COURSE  ***" << endl 
     << endl;

CourseInfo c3, c4;
string id3;

cout << "Enter the course id to be checked: ";
cin >> id3;
c3.setCourseId(id3);

c4 = s1.checkCourseTaken(s1.getZid(), c3);

cout << endl << "Test 6, check if course was taken:"  << endl;

if(c4.getCourseId() == "")
  cout << endl << "***  COURSE NOT TAKEN  ***" << endl << endl;

else
  cout << endl << "Student took " << c4.getCourseId() << " in the " 
       << c4.getSemesterToTake() << " of " << c4.getYearToTake() 
       << " and earned a grade of " << c4.getScoreObtained() << "." << endl 
       << endl; 

cout << "***  END OF PROGRAM OUTPUT  ***" << endl;
*/
return 0;
}//end main
John Smith 333222333 01/01/80 M z11119
cs111 2008 fall a
#include "Person.h"

/****************************************************************
   FUNCTION:   Person()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the Person() constructor
****************************************************************/
Person::Person()
{
}

/****************************************************************
   FUNCTION:   ~Person()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the Person destructor
****************************************************************/      
Person::~Person()
{
}

/****************************************************************
   FUNCTION:   setFirstName(const string &x)
   ARGUMENTS:  const string &x - the first name of student
   RETURNS:    none
   NOTES:      this function sets the students first name
****************************************************************/
void Person::setFirstName(string &x)
{
     firstName = x;
}

/****************************************************************
   FUNCTION:   getFirstname() const
   ARGUMENTS:  none
   RETURNS:    string firstName
   NOTES:      this function returns the students first name
****************************************************************/
string Person::getFirstName() const 
{
     return firstName;
}

/****************************************************************
   FUNCTION:   setLastName(const string &x)
   ARGUMENTS:  const string &x - the students last name
   RETURNS:    none
   NOTES:      this function sets the students last name
****************************************************************/
void Person::setLastName(string &x) 
{
     lastName = x;
}

/****************************************************************
   FUNCTION:   getLastName() const
   ARGUMENTS:  none
   RETURNS:    string lastName
   NOTES:      this function returns the students last name
****************************************************************/
string Person::getLastName() const 
{
     return lastName;
}

/****************************************************************
   FUNCTION:   setSSN(const string &x)
   ARGUMENTS:  const string &x
   RETURNS:    none
   NOTES:      this function sets the students SSN
****************************************************************/
void Person::setSSN(string &x) 
{
     SSN = x;
}

/****************************************************************
   FUNCTION:   getSSN() const
   ARGUMENTS:  none
   RETURNS:    string SSN
   NOTES:      this function returns the students SSN
****************************************************************/
string Person::getSSN() const 
{
       return SSN;
}

/****************************************************************
   FUNCTION:   setDOB(const string &x)
   ARGUMENTS:  const string &x - the students DOB
   RETURNS:    none
   NOTES:      this function sets the students DOB
****************************************************************/
void Person::setDOB(string &x) 
{
     DOB = x;
}

/****************************************************************
   FUNCTION:   getDOB() const
   ARGUMENTS:  none
   RETURNS:    string DOB
   NOTES:      this function returns the students DOB
****************************************************************/
string Person::getDOB() const 
{
       return DOB;
}

/****************************************************************
   FUNCTION:   setGender(const string &x)
   ARGUMENTS:  const string &x - the students gender
   RETURNS:    none
   NOTES:      this function sets the student gender
****************************************************************/
void Person::setGender(string &x) 
{
     gender = x;
}

/****************************************************************
   FUNCTION:   getGender() const
   ARGUMENTS:  none
   RETURNS:    string gender 
   NOTES:      this function returns the students gender
****************************************************************/
string Person::getGender() const 
{
       return gender;
}
/****************************************************************
   FILE:      Person.h
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/23/2009

   PURPOSE:   Includes the class Person information / prototypes 
****************************************************************/
#ifndef PERSON_H
#define PERSON_H

#include "340.h"

class Person {
	public:
      Person();
      virtual ~Person();
      void setFirstName(string &x);
      string getFirstName() const;
      void setLastName(string &x);
      string getLastName() const;
      void setSSN(string &x);
      string getSSN() const;
      void setDOB(string &x);
      string getDOB() const;
      void setGender(string &x);
      string getGender() const;
      
    private:
      string firstName;
      string lastName;
      string SSN;
      string DOB;
      string gender;
};

#endif //PERSON_H
/****************************************************************
   FILE:      SinglyLinkedList.h
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/23/2009

   PURPOSE:   Includes the class SinglyLinkedList information / prototypes 
****************************************************************/
#ifndef SINGLYLINKEDLIST_H
#define SINGLYLINKEDLIST_H

#include "340.h"

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




template <class T>
class SinglyLinkedList
{
	public:
		SinglyLinkedList();
		~SinglyLinkedList();
		bool isEmpty();
		void addToHead(T);

	private:
		int count; // variable stores # of elements in list
		nodeSLL<T> *head; //pointer to first node in list
		nodeSLL<T> *tail; //pointer to last node in list

};

template <class T>
SinglyLinkedList<T>::SinglyLinkedList()
{
	head = NULL;
}

template <class T>
SinglyLinkedList<T>::~SinglyLinkedList()
{
}

template <class T>
bool SinglyLinkedList<T>::isEmpty()
{
	if (head == NULL)
		return true;
	else
		return false;
}

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

#endif
#include "Student.h"


/****************************************************************
   FUNCTION:   Student()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the Student class constructor
****************************************************************/
Student::Student()
{
	count = 0;
}

/****************************************************************
   FUNCTION:   ~Student()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the destructor for the Student class
****************************************************************/   
Student::~Student()
{
}

/****************************************************************
   FUNCTION:   incrementCount()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      This function increments count so that the number
               of classes added are counted on entry
****************************************************************/
void Student::incrementCount()
{
	count = count + 1;
}

/****************************************************************
   FUNCTION:   getZID()
   ARGUMENTS:  none
   RETURNS:    ZID - returns the students ZID number
   NOTES:      This function simply returns the students ZID#
****************************************************************/
string Student::getZID() const
{
     return ZID;
}

/****************************************************************
   FUNCTION:   setZID(const string &x)
   ARGUMENTS:  string x
   RETURNS:    none
   NOTES:      this function sets the students ZID with the value
               passed to it.
****************************************************************/
void Student::setZID(string &x)
{
     ZID = x;
}

/****************************************************************
   FUNCTION:   getCount() const
   ARGUMENTS:  none
   RETURNS:    int count
   NOTES:      this function returns the count of the number of 
               classes added to the students registration etc
****************************************************************/
int Student::getCount() const
{
	return count;
}

/****************************************************************
   FUNCTION:   infoPrint()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this prints the students information, including the
               basic information, aswell as class information
****************************************************************/
void Student::infoPrint()
{	
	cout << "First name: " << getFirstName()<< endl;
	cout << "Last name: " << getLastName() << endl;
	cout << "SSN: " << getSSN() << endl;
	cout << "DOB: " << getDOB() << endl;
	cout << "Gender: " << getGender()<< endl;
	cout << "ZID: " << getZID() << endl;    
  
 /*
    
	int count = 0;          
	while (count <= getCount())
           {
             cout << setw(8) << courses[count].getCourseID() << " " 
                << setw(5) << courses[count].getYearTaken() << " " 
                << setw(7) << courses[count].getSemesterTaken() << " " 
                << setw(3) << courses[count].getGrade() << endl;
             count++;
           }
*/
}

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;
	}	
  }
/****************************************************************
   FILE:      Student.h
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/23/2009

   PURPOSE:   Includes the class Student information / prototypes 
****************************************************************/
#ifndef STUDENT_H
#define STUDENT_H

#include "Person.h"
#include "courseInfo.h"
#include "SinglyLinkedList.h"
#include "340.h"

class Student : public Person {
	public:
      Student();
      virtual ~Student(); 
      string getZID() const;
      void setZID(string &x);    
      void incrementCount();
      int getCount() const;
      void infoPrint(); 
	  void loadInfoFromFile(fstream& inFile);
      SinglyLinkedList<CourseInfo> courseList;

    private:
      string ZID;
      int count;
     

};

#endif //STUDENT_H

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.

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

Attachments
#ifndef H_340
#define H_340

#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>

#include <cstdlib>
#include <climits>
#include <cmath>
#include <ctime>

#include <algorithm>
#include <functional>
#include <iterator>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>

#include <stdexcept>
using namespace std;
#endif
#include "courseInfo.h"

/****************************************************************
   FUNCTION:   courseInfo()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the constructor for the CourseInfo class
****************************************************************/
CourseInfo::CourseInfo()
{
   
   count = 0;
}

/****************************************************************
   FUNCTION:   ~CourseInfo()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the destructor for the CourseInfo class
****************************************************************/
CourseInfo::~CourseInfo()
{
}

/****************************************************************
   FUNCTION:   getCourseID() const
   ARGUMENTS:  none
   RETURNS:    string courseID
   NOTES:      this function returns the courseID for the student
****************************************************************/
string CourseInfo::getCourseID() const
{
	return courseID;
}

/****************************************************************
   FUNCTION:   setCourseID(const string &x)
   ARGUMENTS:  const string &x
   RETURNS:    none
   NOTES:      this function sets the courseID for the student
****************************************************************/
void CourseInfo::setCourseID(string &x)
{
	courseID = x;
}

/****************************************************************
   FUNCTION:   setYearTaken(const string &x)
   ARGUMENTS:  const string &x - the new YearTaken
   RETURNS:    none
   NOTES:      this function sets the students YearTaken for a course
****************************************************************/
void CourseInfo::setYearTaken(string &x)
{
	yearTaken = x;
}

/****************************************************************
   FUNCTION:   getYearTaken() const
   ARGUMENTS:  none
   RETURNS:    string yearTaken
   NOTES:      this returns the students YearTaken for a certain class
****************************************************************/
string CourseInfo::getYearTaken() const
{
	return yearTaken;
}

/****************************************************************
   FUNCTION:   setSemesterTaken(const string &x)
   ARGUMENTS:  const string &x - the semesterTaken value
   RETURNS:    none
   NOTES:      this function sets the students semester taken for
               a specific course
****************************************************************/
void CourseInfo::setSemesterTaken(string &x)
{
	semesterTaken = x;
}

/****************************************************************
   FUNCTION:   getSemesterTaken() const
   ARGUMENTS:  none
   RETURNS:    string semesterTaken - returns the semester taken
   NOTES:      this function gets the students semester taken
****************************************************************/
string CourseInfo::getSemesterTaken() const
{
	return semesterTaken;
}

/****************************************************************
   FUNCTION:   setGrade(const string &x)
   ARGUMENTS:  const string &x
   RETURNS:    none
   NOTES:      this functions sets the grade for a certain class
****************************************************************/     
void CourseInfo::setGrade(string &x)
{
	grade = x;
}
      
/****************************************************************
   FUNCTION:   getGrade() const
   ARGUMENTS:  none
   RETURNS:    string grade
   NOTES:      this function returns the grade of the student in 
                a specific class
****************************************************************/
string CourseInfo::getGrade() const
{
	return grade;
}

/****************************************************************
   FUNCTION:   incrementCount(int &x)
   ARGUMENTS:  int x - the value of count
   RETURNS:    none
   NOTES:      this function increments the number of courses input
****************************************************************/
void CourseInfo::incrementCount(int &x)
{
	x = x + 1;
}

/****************************************************************
   FUNCTION:   getCoount() const
   ARGUMENTS:  none
   RETURNS:    int count
   NOTES:      this function returns the count of classes
****************************************************************/
int CourseInfo::getCount() const
{
	return count;
}
/****************************************************************
   FILE:      courseInfo.h
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/23/2009

   PURPOSE:   Includes the class courseInfo information / prototypes 
****************************************************************/
#ifndef COURSEINFO_H
#define COURSEINFO_H

#include "340.h"

class CourseInfo  {
	public:
      CourseInfo();
      virtual ~CourseInfo();
      void setCourseID(string &x);
      string getCourseID() const;
      void setYearTaken(string &x);
      string getYearTaken() const;
      void setSemesterTaken(string &x);
      string getSemesterTaken() const;
      void setGrade(string &x);
      string getGrade() const;
      void incrementCount(int &x);
      int getCount() const;
      
    private:
      string courseID;
      string yearTaken;
      string semesterTaken;
      string grade;
      int count;
      
};


#endif //COURSEINFO_H
/****************************************************************
   PROGRAM:   Project 1
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/26/2009

   FUNCTION:  creates a program that enables a user to input
   		    student data. Including name, zid, SSN, and courses.

   INPUT:     from keyboard

   OUTPUT:    A list of all courses taken/schedules for a certain
   			  student.

   NOTES:     Uses 6 files. Student.h, Student.cpp, Person.h, Person.cpp,
   			  courseInfo.h, courseInfo.cpp, and 340.h.
****************************************************************/
#include "Person.h"
#include "CourseInfo.h"
#include "Student.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
//create Student object and declare variables to be used in creating student
Student s1;
bool empty;
string file;
fstream inFile;

//test if list is empty before data is filled

cout << endl << "Test 1, Check if list is empty:" << endl;
empty = s1.courseList.isEmpty();
cout << "Empty = " << empty;
if(empty == true)
  cout << "Student Object is empty" << endl;
else
  cout << "Student Object is not empty"  << endl;

//read data from file and fill the list with courses

inFile.open("C:\\Documents and Settings\\Tony\\My Documents\\Visual Studio 2008\\Projects\\proj1\\file.txt");

if(!inFile)
  {
  cout << "Can't open file , program terminated." << endl;
  return 0;
  }

s1.loadInfoFromFile(inFile);
inFile.close();


cout << endl << "Test 2, Check if list was filled:" << endl;
s1.infoPrint();

//check if list is empty after data has been filled

cout << endl << "Test 3, Check if list is empty:" << endl;
empty = s1.courseList.isEmpty();
if(empty == true)
  cout << "Student Object is empty" << endl;
else
  cout << "Student Object is not empty"  << endl;

//check to see if you can successfully add a course to the list
cout << endl << "***  ADD A COURSE TO THE STUDENT  ***" << endl << endl;
CourseInfo c1;
string id1, yt1, st1, so1;

cout << "Enter course id to add: ";
cin >> id1;
c1.setCourseID(id1);

cout << "Enter year the course was taken: ";
cin >> yt1;
c1.setYearTaken(yt1);

cout << "Enter semester the course was taken: ";
cin >> st1;
c1.setSemesterTaken(st1);

cout << "Enter grade earned in the course: ";
cin >> so1;
c1.setGrade(so1);

s1.addCourse(s1.getZID(), c1);

cout << endl << "Test 4, check if course was added:" << endl;
s1.infoPrint();
/*
//check to see if you can remove a course from the list
cout << endl << "***  REMOVE A COURSE FROM A STUDENT ***" << endl << endl;

CourseInfo c2;
string id2, yt2, st2, so2;

cout << "Enter course id to remove: ";
cin >> id2;
c2.setCourseId(id2);

cout << "Enter year the course was taken: ";
cin >> yt2;
c2.setYearToTake(yt2);

cout << "Enter semester the course was taken: ";
cin >> st2;
c2.setSemesterToTake(st2);

s1.deleteCourse(s1.getZid(), c2);

cout << endl << "Test 5, check if course was removed:" << endl;
s1.infoPrint();

//check if a certain course exists in the list
cout << endl << "***  CHECK IF A STUDENT HAS TAKEN A COURSE  ***" << endl 
     << endl;

CourseInfo c3, c4;
string id3;

cout << "Enter the course id to be checked: ";
cin >> id3;
c3.setCourseId(id3);

c4 = s1.checkCourseTaken(s1.getZid(), c3);

cout << endl << "Test 6, check if course was taken:"  << endl;

if(c4.getCourseId() == "")
  cout << endl << "***  COURSE NOT TAKEN  ***" << endl << endl;

else
  cout << endl << "Student took " << c4.getCourseId() << " in the " 
       << c4.getSemesterToTake() << " of " << c4.getYearToTake() 
       << " and earned a grade of " << c4.getScoreObtained() << "." << endl 
       << endl; 

cout << "***  END OF PROGRAM OUTPUT  ***" << endl;
*/
return 0;
}//end main
John Smith 333222333 01/01/80 M z11119
cs111 2008 fall a
cs222 2008 spring b
#include "Person.h"

/****************************************************************
   FUNCTION:   Person()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the Person() constructor
****************************************************************/
Person::Person()
{
}

/****************************************************************
   FUNCTION:   ~Person()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the Person destructor
****************************************************************/      
Person::~Person()
{
}

/****************************************************************
   FUNCTION:   setFirstName(const string &x)
   ARGUMENTS:  const string &x - the first name of student
   RETURNS:    none
   NOTES:      this function sets the students first name
****************************************************************/
void Person::setFirstName(string &x)
{
     firstName = x;
}

/****************************************************************
   FUNCTION:   getFirstname() const
   ARGUMENTS:  none
   RETURNS:    string firstName
   NOTES:      this function returns the students first name
****************************************************************/
string Person::getFirstName() const 
{
     return firstName;
}

/****************************************************************
   FUNCTION:   setLastName(const string &x)
   ARGUMENTS:  const string &x - the students last name
   RETURNS:    none
   NOTES:      this function sets the students last name
****************************************************************/
void Person::setLastName(string &x) 
{
     lastName = x;
}

/****************************************************************
   FUNCTION:   getLastName() const
   ARGUMENTS:  none
   RETURNS:    string lastName
   NOTES:      this function returns the students last name
****************************************************************/
string Person::getLastName() const 
{
     return lastName;
}

/****************************************************************
   FUNCTION:   setSSN(const string &x)
   ARGUMENTS:  const string &x
   RETURNS:    none
   NOTES:      this function sets the students SSN
****************************************************************/
void Person::setSSN(string &x) 
{
     SSN = x;
}

/****************************************************************
   FUNCTION:   getSSN() const
   ARGUMENTS:  none
   RETURNS:    string SSN
   NOTES:      this function returns the students SSN
****************************************************************/
string Person::getSSN() const 
{
       return SSN;
}

/****************************************************************
   FUNCTION:   setDOB(const string &x)
   ARGUMENTS:  const string &x - the students DOB
   RETURNS:    none
   NOTES:      this function sets the students DOB
****************************************************************/
void Person::setDOB(string &x) 
{
     DOB = x;
}

/****************************************************************
   FUNCTION:   getDOB() const
   ARGUMENTS:  none
   RETURNS:    string DOB
   NOTES:      this function returns the students DOB
****************************************************************/
string Person::getDOB() const 
{
       return DOB;
}

/****************************************************************
   FUNCTION:   setGender(const string &x)
   ARGUMENTS:  const string &x - the students gender
   RETURNS:    none
   NOTES:      this function sets the student gender
****************************************************************/
void Person::setGender(string &x) 
{
     gender = x;
}

/****************************************************************
   FUNCTION:   getGender() const
   ARGUMENTS:  none
   RETURNS:    string gender 
   NOTES:      this function returns the students gender
****************************************************************/
string Person::getGender() const 
{
       return gender;
}
/****************************************************************
   FILE:      Person.h
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/23/2009

   PURPOSE:   Includes the class Person information / prototypes 
****************************************************************/
#ifndef PERSON_H
#define PERSON_H

#include "340.h"

class Person {
	public:
      Person();
      virtual ~Person();
      void setFirstName(string &x);
      string getFirstName() const;
      void setLastName(string &x);
      string getLastName() const;
      void setSSN(string &x);
      string getSSN() const;
      void setDOB(string &x);
      string getDOB() const;
      void setGender(string &x);
      string getGender() const;
      
    private:
      string firstName;
      string lastName;
      string SSN;
      string DOB;
      string gender;
};

#endif //PERSON_H
/****************************************************************
   FILE:      SinglyLinkedList.h
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/23/2009

   PURPOSE:   Includes the class SinglyLinkedList information / prototypes 
****************************************************************/
#ifndef SINGLYLINKEDLIST_H
#define SINGLYLINKEDLIST_H

#include "340.h"

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


template <class T>
class SinglyLinkedList
{
	public:
		SinglyLinkedList();
		~SinglyLinkedList();
		bool isEmpty();
		void addToHead(T);

	
		int count; // variable stores # of elements in list
		nodeSLL<T> *head; //pointer to first node in list
		nodeSLL<T> *tail; //pointer to last node in list

};

template <class T>
SinglyLinkedList<T>::SinglyLinkedList()
{
	head = NULL;
}

template <class T>
SinglyLinkedList<T>::~SinglyLinkedList()
{
}

template <class T>
bool SinglyLinkedList<T>::isEmpty()
{
	if (head == NULL)
		return true;
	else
		return false;
}

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

#endif
#include "Student.h"


/****************************************************************
   FUNCTION:   Student()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the Student class constructor
****************************************************************/
Student::Student()
{
	count = 0;
}

/****************************************************************
   FUNCTION:   ~Student()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this is the destructor for the Student class
****************************************************************/   
Student::~Student()
{
}

/****************************************************************
   FUNCTION:   incrementCount()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      This function increments count so that the number
               of classes added are counted on entry
****************************************************************/
void Student::incrementCount()
{
	count = count + 1;
}

/****************************************************************
   FUNCTION:   getZID()
   ARGUMENTS:  none
   RETURNS:    ZID - returns the students ZID number
   NOTES:      This function simply returns the students ZID#
****************************************************************/
string Student::getZID() const
{
     return ZID;
}

/****************************************************************
   FUNCTION:   setZID(const string &x)
   ARGUMENTS:  string x
   RETURNS:    none
   NOTES:      this function sets the students ZID with the value
               passed to it.
****************************************************************/
void Student::setZID(string &x)
{
     ZID = x;
}

/****************************************************************
   FUNCTION:   getCount() const
   ARGUMENTS:  none
   RETURNS:    int count
   NOTES:      this function returns the count of the number of 
               classes added to the students registration etc
****************************************************************/
int Student::getCount() const
{
	return count;
}

/****************************************************************
   FUNCTION:   infoPrint()
   ARGUMENTS:  none
   RETURNS:    none
   NOTES:      this prints the students information, including the
               basic information, aswell as class information
****************************************************************/
void Student::infoPrint()
{	
	cout << getFirstName() << " " << getLastName() << " "
		 << getSSN() << " " << getDOB() << " " << getGender() 
		 << " " << getZID() << endl;  
  
	//cout << courseList.nodeSLL->info;
	while (courseList.head->next != NULL)
	{
		cout << courseList.head->info.getCourseID() 
			 << courseList.head->info.getSemesterTaken()
			 << courseList.head->info.getYearTaken()
			 << courseList.head->info.getGrade() << endl;
		courseList.head = courseList.head->next;
	}
 /*
    

	int count = 0;          
	while (count <= getCount())
           {
             cout << setw(8) << courses[count].getCourseID() << " " 
                << setw(5) << courses[count].getYearTaken() << " " 
                << setw(7) << courses[count].getSemesterTaken() << " " 
                << setw(3) << courses[count].getGrade() << endl;
             count++;
           }
*/
}

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);

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

void Student::addCourse(string ZID, const CourseInfo& c1)
{
	cout << "IN ADD COURSE!!!" << endl;
	courseList.addToHead(c1);
}
/****************************************************************
   FILE:      Student.h
   AUTHOR:    Anthony Hogan
   LOGON ID:  Z109079
   DUE DATE:  1/23/2009

   PURPOSE:   Includes the class Student information / prototypes 
****************************************************************/
#ifndef STUDENT_H
#define STUDENT_H

#include "Person.h"
#include "courseInfo.h"
#include "SinglyLinkedList.h"
#include "340.h"

class Student : public Person {
	public:
      Student();
      virtual ~Student(); 
      string getZID() const;
      void setZID(string &x);    
      void incrementCount();
      int getCount() const;
      void infoPrint(); 
	  void loadInfoFromFile(fstream& inFile);
      SinglyLinkedList<CourseInfo> courseList;
	  void addCourse(string, const CourseInfo &);

    private:
      string ZID;
      int count;
     

};

#endif //STUDENT_H
This article has been dead for over six months. Start a new discussion instead.