-
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
-
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?
-
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
-
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.
-
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.
-
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.
-
I did what u said b4 u edited the post lol and it works fine i didnt realize that thanks alot.