Thread: runtime error (leak?)

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    runtime error (leak?)

    My program crashes on like the 3rd line. It outputs the first 3 lines fine , but when it gets to actually modeling thats when it crashes. I cant figure it out in the debugger.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    class Section;
    class LabWorker;
    class TimeSlot;
    class StudentRecord;
    
    
    class TimeSlot
    {
    	public:
    		TimeSlot(string day,unsigned hour):day(day),hour(hour)
    		{}
    		void display(ostream & out)const;
    	private:
    		string day;
    		unsigned hour;
    };
    
    
    void TimeSlot::display(ostream & out)const
    {
    	out<<day<<" "<<hour;
    }
    class LabWorker
    {
    	public:
    		LabWorker(string name):name(name){}
    		void addSection(Section * sect);
    		void addGrade(string name,int grade,unsigned week);
    		void displayAverageForWeek(unsigned week)const;
    	private:
    		string name;
    		Section* assignedSection;
    };
    class StudentRecord
    {
    	public:
    		StudentRecord(string name):name(name),grades(13,-1)
    		{}
    		string getName(){return name;}
    		void setGrade(int grade,unsigned week);
    		vector <int> getGrades(){return grades;}
    	private:
    		string name;
    		vector <int> grades;
    };
    
    class Section
    {
    	friend ostream& operator <<(ostream&,const Section&);
    	
    	public:
    		Section(string ID, string day,unsigned hour):sectionID(ID),slot(day,hour)
    		{}
    		void addInstructor(LabWorker instr);
    		void loadStudentsFromFile(string file);
    		void displayStudents();
    		StudentRecord* getStudentRecord(string name);
    		vector <StudentRecord*> getRecords()const{return records;}
    	private:
    		string sectionID;
    		TimeSlot slot;
    		LabWorker* instructor;
    		vector <StudentRecord*> records;
    };
    
    StudentRecord* Section::getStudentRecord(string name)
    {
    	for(size_t i=0;i<records.size();i++)
    	{
    		if(records[i]->getName() == name)
    			return records[i];
    	}
    	return NULL;
    }
    void Section::displayStudents()
    {
    	for(size_t i=0;i<records.size();i++)
    	{
    		cout << records[i]->getName() << "\n";
    	}
    }
    void Section::loadStudentsFromFile(string file)
    {
    	ifstream infile;
    	infile.open(file.c_str());
    	if(!infile)
    	{
    		cerr<<"Error Terminating Program\n";
    		exit(1);
    	}
    	string name;
    	while(infile>>name)
    	{
    		records.push_back(new StudentRecord(name));
    	}
    }
    void Section::addInstructor(LabWorker instr)
    {
    	instructor = &instr;
    	instructor ->addSection(this);
    }
    void StudentRecord::setGrade(int grade,unsigned week)
    {
    	grades[week]=grade;
    }
    
    ostream & operator <<(ostream& out,const Section& sect)
    {
    	out<<sect.sectionID;
    	sect.slot.display(out);
    	return out;
    }
    void LabWorker::displayAverageForWeek(unsigned week)const
    {
    	int grades(0),count(0);
    	vector <StudentRecord*> rcrds=assignedSection->getRecords();
    	
    	for(size_t i=0;i<rcrds.size();i++)
    	{
    		vector <int> gds = rcrds[i]->getGrades();
    		grades += gds[week];
    		count++;
    	}
    	cout<<"The average for week number "<<week<<" is "<<grades/count;
    }
    void LabWorker::addSection(Section * sect)
    {
    	assignedSection = sect;
    }
    void LabWorker::addGrade(string name,int grade,unsigned week)
    {
    	StudentRecord* temp = assignedSection->getStudentRecord(name);
    	if(!temp)
    		cerr<<"Student doesn't exist\n";
    	else
    		temp->setGrade(grade,week);
    }
    
    int main() 
    {
       // lab workers
       LabWorker joe( "Joe" );
       LabWorker jane( "Jane" );
       
       // sections and setup
       Section secA2( "A2", "Tuesday", 4 );
       secA2.addInstructor( joe );
       secA2.loadStudentsFromFile( "A2Data.txt" );
       //secA2.display();
       cout << secA2 << endl;
       Section secB3( "B3", "Thursday", 11 );
       secB3.addInstructor( jane );
       secB3.loadStudentsFromFile( "sectData.txt" );
       //secB3.display();
       cout << secB3 << endl;
    
       // week one activities
       cout << "Modeling week: 1\n";
       joe.addGrade( "Bob", 7, 1 );  
       joe.addGrade( "Tania", 9, 1 );  
       joe.addGrade( "Joe", 7, 1 );  
       joe.addGrade( "Sue", 7, 1 );  
       cout << "End of week one\n";
       joe.displayAverageForWeek(1);
    
       // week two activities
       cout << "Modeling week: 1\n";
       joe.addGrade( "Tania", 5, 2 );  
       joe.addGrade( "Bob", 10, 2 );  
       joe.addGrade( "Sue", 0, 2 );  
       cout << "End of week two\n";
       joe.displayAverageForWeek(2);
    
       // test that week 1 still works
       cout << endl;
       joe.displayAverageForWeek(1);
    
    }
    I posted the whole code because i dont know exactly whats wrong. Here is a link to the problem in case you need to check what it should print and it also has the two files.

    http://cis.poly.edu/cs1124/Recitatio...on5/rec05.html
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Looks like you need to call addSection before you call addGrade. Although that looks a bit dangerous because what if the object that you pass to addSection goes out of scope/is destroyed before the LabWorker object?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    maybe i should have also mentioned i cant change main.. main was given to us .. our goal was to make a class that allowed main to work
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why are the member variables assignedSection and instructor pointers? Notice that in main a pointer isn't passed to the add methods.

    I don't see a reason to use pointers at all here. If you remove them (and update everything else to compile properly), your code just might work.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Thats actually how are TA told us to do it. I dont c how to do it without it if the classes are associated with each other.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    OK, you're right. Didn't notice that.

    Only one of those needs to be a pointer to avoid the circular usage, the other can be a regular object. Try just making the instructor a regular object.
    Last edited by Daved; 10-17-2005 at 07:51 PM.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I did what u said b4 u edited the post lol and it works fine i didnt realize that thanks alot.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  2. link with C runtime library
    By George2 in forum C++ Programming
    Replies: 26
    Last Post: 02-05-2008, 01:56 AM
  3. Visual Studio and .Net Runtime Framework dependency
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-08-2007, 07:52 AM
  4. Change a control class during runtime
    By Joelito in forum Windows Programming
    Replies: 3
    Last Post: 01-12-2006, 02:13 PM
  5. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM