Hi all,

I recently finished working on a project that was to create a database type system for a student registration system. I created my own list class and stack to store the data... i create multiple classes, person (store student data: name, ssn, etc), student (student ID number), courseInfo (class name, grade, semester, year).

I now have to be able to read in from file (just like before) but this time i need to read in multiple students. As follows:

John Smith 333222333 01/01/89 M z11119
CSCI340 2008 Fall A
CSCI231 2008 Fall B
Linda Ray 333222333 01/01/88 M z11203
CSCI340 2008 Fall B
CSCI201 2008 Fall B
Rene Hanson 333222333 01/01/81 M z22003
CSCI340 2007 Fall A
CSCI210 2008 Spring B

First off, i am going to use a marker in the file above, such as a '$' to denote end of a student data... therefore when i do the read in, and i hit '$' i know that is the end of the that students data.

My problem arises in trying to figure out how i can read in multiple students. As of now i do the following:

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

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

	//start class read
	inFile >> courseID;
	
	while (inFile)
	{
	inFile >> year;
	inFile >> semester;
	inFile >> grade;
		 
	CourseInfo courses;
	courses.setCourseID(courseID);
	courses.setYearTaken(year);
	courses.setSemesterTaken(semester);
	courses.setGrade(grade);
	
	addCourse(getZID(), courses);
	inFile >> courseID;
//check for courseID == '$' ????
  }

That is how i read in from file and it works flawlessly for one student. How then, can i edit this to work with multiple students. I ask this because how would i set up my data type? Do i create a list that contains each student per node? So i read in student one....fill in that persons student info, and class list, then store that in the first node of a linked list. Then read in the second student, ...then store as second node in my linked list?

If this is a way it could be done, could someone give me some psuedo code, because right now i am at a complete loss.

Thank you

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

I think the trick here is the lines.

Using your example, a new student - occurs every three lines...

So, read in the file one line at a time. Use the % operator or a simple counter to know when you hit the third line.

Each line you read can be split by a space. Stringstreams would do the job nicely.

...and you could have a vector of student objects.

Thats a good idea, but the file i use may be different to the one being sued to grade the project. It will not be true that each student has 3 classes etc. Therefore, i need to use a system that could take 10 classes for one student, 3 for another, etc.

Also, you mention using a vector, would you mean that i create a vector and whenever i create a new student, that student object is stored in the vector, therefore, later on, i should be able to search through each student object within the object searching for a certain class etc.

Any more ideas would be great. thanks.

Member Avatar for iamthwee

Ok, so one way would be to find out where the classes end and a student name begins. All lines with classes have exactly three spaces? That's a clue.

And I would consider the student definition to have an array of classes.

So one student object would have an array of classes:

CSCI340 2007 Fall A
CSCI210 2008 Spring B

associated with it. That would be simple enough to reference.

Also, i am trying to work with the function i supplied above... If the courseID == "$" then you reach the end of the current students data...how then, can i set it up so that a new Student object is created...because working within the that readinfromfile() function, i the only stuff i edit is the current student object, what would i have to change to make this work with multiple students? Do i NOT call it using s1.readinfromfile(inFile) (s1 is the student object created in driver.cpp). But i cant do that because then the print function would not know what to work with.

Could i just move the readinfromfile() function out of the Student class, and just put it in the driver.cpp?

Then i would be able to loop and create new student info data stuff etc more easily.

Make sense? Any help as always would be awesome.

Member Avatar for iamthwee

I would forget this nonsense about using a $ as a marker.
Re-read post #4 this should be a piece of cake.

I am still working on this, and have come up with this function that will read in from file multiple students. I know you have said that i should not deal with a marker, but my teacher pretty much said to do it (for whatever reason....). therefore, i am using my $ as my marker, but currently have a problem with the reading in from file in that after the first student, the data gets garbled. My text file looks like this:

John Smith 333222333 01/01/89 M z11119
CSCI340 2008 Fall A
CSCI231 2008 Fall B
CSCI444 2009 Spring A
CSCI333 2006 Fall B
$
Linda Ray 333222333 01/01/88 M z11203
CSCI340 2008 Fall B
CSCI201 2008 Fall B
CSCI333 2008 Spring A
$
Rene Hanson 333222333 01/01/81 M z22003
CSCI340 2007 Fall A
CSCI210 2008 Spring B

And my function is this: (sorry for all the couts, but this was used to see what the output was).

void Registration::loadInfoFromFile()
{
	Student s1;
	fstream inFile;
	string file;

	cout << endl << "Enter file name for student data: ";
	cin >> file;

	inFile.open(file.c_str());

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

	string firstName, lastName, SSN, DOB, gender, 
		   ZID, courseID, year, semester, grade;

	while (inFile)
	{
	inFile >> firstName;
	s1.setFirstName(firstName);
	cout << firstName << " ";
	inFile >> lastName;
	s1.setLastName(lastName);
	cout << lastName << " ";
	inFile >> SSN;
	s1.setSSN(SSN);
	cout << SSN << " ";
	inFile >> DOB;
	s1.setDOB(DOB);
	cout << DOB << " ";
	inFile >> gender;
	s1.setGender(gender);
	cout << gender << " ";
	inFile >> ZID;
	s1.setZID(ZID);
	cout << ZID << " ";
	//start class read
	inFile >> courseID;
	
	while (inFile && inFile != "$")
	{
		inFile >> year;
		inFile >> semester;
		inFile >> grade;
		 
		CourseInfo courses;
		courses.setCourseID(courseID);
		cout << "\n" << courseID << " "; 
		courses.setYearTaken(year);
		cout << year << " ";
		courses.setSemesterTaken(semester);
		cout << semester << " ";
		courses.setGrade(grade);
		cout << grade << endl;
	
		s1.addCourse(s1.getZID(), courses);
		inFile >> courseID;
		if (courseID == "$")
		{
			cout << "push into list" << endl;
			students.push_front(s1);
			inFile >> courseID;
		}
	}
}
	inFile.close();
}

My output gets garbled in that it looks something like this:

John Smith 333222333 01/01/89 M z111119
csci340 2008 fall a
csci231 2008 fall b
csci444 2009 spring a
csci333 2006 fall b
push into list

linda ray 333222333 01/01/88
M z112303 csci340 20008
fall B csci201 20008
fall B csci333 2008
spring A $ Rene

hanson 333222333 01/01/81 M
z22003 csci340 2007 fall
...
...

Therefore, where do you think my issue is here. I have tried moving some of the inFile's around but i cant get it working quite right. The other thing i was thinking is that my while conditions might be off base aswell.

Any help would be appreciated!

Nevermind. Got it working. Had to change my 2nd while statement to have courseID == "$" and then removed the if statement at the end of the while. Will have other issues, so will leave post as unsolved for now.

Thanks all

Have uploaded my code for review. I am having a problem within my Registration.h file. This is because for whatever reason i cannot use a function call, getGrade() because i do not have access to it (not a part of my student class ... its a part of my courseInfo class).

How can i get access to these functions so that i can use them for a search/comparison within the Registration.h file.

Thanks

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.