Need some help here. I am outputting a course and student report. Student report shows what classes the student is in, while Class report shows what students are in a certain class

Ok, so class report is only showing the classes (including professor name and classroom #), but not the students in them. however the Student report works like it should, displaying the student and what classes they have registered for, but the professor name or classroom number isn't there.

Everything compiles and runs, so it has to be a mixed up variable or something simple somewhere, some expert eyes are needed. BTW, I'm pretty sure I have narrowed it down to fillCourses() not filling up the <Student *> students list.

Here is the necessary code:

Wrapper.h

class Wrapper
{
	public:
		Wrapper();
		Wrapper(list <Course *> course, list <Student *> student);
		void readStudents(istream & infile);
		void fillCourses();
		Student * findStudent (string & searchName);
		Course * findCourse (string & searchName);
		string getwname() { return wname; };
		void readCourses(istream & infile);
		void generateCourseReports();
		void generateStudentReports();
	protected:
		string wname;
		string professor;
		string classroom;
		list <Student *> all_students;
		list <Course *> all_courses;
};

Wrapper::Wrapper()
{
	all_students;
	all_courses;
}

void Wrapper::readCourses(istream & infile)
{
	string name;
	int max;
	string professor;
	string classroom;

	while (infile >> name >> max >> professor >> classroom)
	{
		Course * newCourse = new Course (name, max, professor, classroom);
		all_courses.push_back(newCourse);	
	}
}

void Wrapper::readStudents(istream & infile)
{
	string name;
	string course;

	while (infile >> name >> course)
	{
		Student * theStudent = findStudent(name);
		Course * theCourse = findCourse(course);	
		
		if (theCourse != 0)
		{
			theStudent -> addCourse(theCourse);
		}
		else
			cout << "student " << name << " requested invalid course " << course << endl;
	}

}

void Wrapper::fillCourses()
{
	list<Student *>::iterator s_start, s_end;
	s_start = all_students.begin();
	s_end = all_students.end();
	
	for ( ; s_start != s_end; ++s_start)
	{
		list<Course *>::iterator c_start, c_end, c_next;
		c_start = (*s_start) -> firstCourse();
		c_end = (*s_start) -> lastCourse();
		for ( ; c_start != c_end; c_start = c_next)
		{
			c_next = c_start; ++c_next;

			if (!(*c_start) -> full())
				(*c_start) -> addStudent(*s_start);
			else
				(*s_start) -> removeCourse(c_start);
		}
	}
}

Student * Wrapper::findStudent (string & searchName)
{
	list <Student *>::iterator start, stop;
	start = all_students.begin();
	stop = all_students.end();
	for ( ; start != stop; ++start)
	{
		if ((*start) -> name() == searchName)
			return *start;
	}
		//if not found, make one
	Student * newStudent = new Student(searchName);
	all_students.push_back(newStudent);
	return newStudent;
}

Course * Wrapper::findCourse (string & searchName)
{
	list <Course *>::iterator start, stop;
	start = all_courses.begin();
	stop = all_courses.end();
	for ( ; start != stop; ++start)
	{
		if ((*start) -> getname() == searchName)
			return *start;
	}
		//if not found, make one
	Course * newCourse = new Course(searchName, NULL);
	return newCourse;
}

void Wrapper::generateCourseReports()
{	
	list <Course *>::iterator start, stop;
        start = all_courses.begin();
        stop = all_courses.end();
        for ( ; start != stop; ++start)
                (*start) -> generateClassList();
}

void Wrapper::generateStudentReports()
{
	list <Student *>::iterator s_start, s_stop;
	list <Course *>::iterator c_start, c_stop;
	s_start = all_students.begin();
	s_stop = all_students.end();
	
	for ( ; s_start != s_stop; ++s_start)
	{
		cout << endl;
		cout << "Class list for " << (*s_start) -> name() << ":" << endl << endl;
		c_start = (*s_start) -> firstCourse();
		c_stop = (*s_start) -> lastCourse();
		for ( ; c_start != c_stop; ++c_start)
			cout << (*c_start) -> getname() << " with professor " << (*c_start) -> getprofessor() << endl;
	}
}

student.h

#ifndef STUDENT_H
#define STUDENT_H
#include "course.h"

using namespace std;

class Course;

class Student
{
        typedef list <Course *>::iterator citerator;

        public:
                Student (string n) : nameText(n) { }
		string getwaitinglist() { return waitinglist; }
                string name() { return nameText; }
                void addCourse (Course * c) { courses.push_back(c); }
                citerator firstCourse() { return courses.begin(); }
                citerator lastCourse() { return courses.end(); }
                void removeCourse (citerator & citr) { courses.erase(citr); }
        protected:
		string waitinglist;
                string nameText;
                list <Course *> courses;
friend class wrapper;
};

#endif

course.h and course.cpp

#ifndef COURSE_H
#define COURSE_H
#include "student.h"

using namespace std;

class Student;

class Course 
{
	friend class Wrapper;

	public:
		Course (string n, int s, string p, string c) : name(n), max(s), professor(p), classroom(c) { }
		Course (string n, int s) : name(n), max(s) { }
		string getname() { return name; }
		string getprofessor() { return professor; }
		string getclassroom() { return classroom; }
		bool full () { return students.size() >= max; }
		void addStudent (Student * s) { students.push_back(s); }
		
		void generateClassList ();
	protected:
		string professor;
		string classroom;
		string name;
		int max;
		list <Student *> students;

};

#endif
bool studentCompare (Student * a, Student * b)
{
	return a -> name() < b -> name();
}

void Course::generateClassList()
{
	students.sort(studentCompare);
	
	cout << endl << "Class list for " << getname() << " with Professor " << getprofessor() << " in class " << getclassroom() << ":" << endl;
	list<Student *>::iterator start, stop;
	start = students.begin();
	stop = students.end();

	for ( ; start != stop; ++start)
		cout << (*start) -> name() << endl;
}

And finally the output of the program:


Class list for ART101 with Professor Ted in class UH200:

Class list for HIST213 with Professor Jim in class CS110:

Class list for MATH412 with Professor Jack in class JBH389:

Class list for CSCI330 with Professor Joe in class JBH146:

Class list for Smith,Amy:

ART101 with professor in classroom
MATH412 with professor in classroom

Class list for Jones,Randy:

HIST213 with professor in classroom
CSCI330 with professor in classroom

Class list for Cobb,Kevin:

MATH412 with professor in classroom
CSCI330 with professor in classroom

Class list for Mo,Jo:

CSCI330 with professor in classroom

Class list for Smith,Kathy:

CSCI330 with professor in classroom
MATH412 with professor in classroom
ART101 with professor in classroom

Class list for No,No:

CSCI330 with professor in classroom
MATH412 with professor in classroom

WaltP commented: Very good first post. Code, tags, output. Nice!!!! +11

I am not seeing anything obviously wrong. When you are cerating the class objects I assume max is not 0, that would cause this behaviour.

I would suggest modifying Course::generateClassList, at least temporarily to output max so you can be sure it is good.

You have something strange going on between lines 83 - 81 with c_next which appears to be a redundent variable.

I would try stepping through Wrapper::fillCourses in a debugger if there is no problem with max.

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.