Hello all.
The class Student in this program is a child class of Person. In it, I overloaded the input and output operators, so that the lines in my int main() function will work properly.
I am having some run time issues involving the input operator in my Student class. Towards the end of the function, when the user is asked to input the courses (name and ID) that they are enrolled in, the program crashes. I'm thinking the problem may be a segmentation fault involving the input operator that I overloaded for the Struct called "Course."
It isn't due for a few more days but any help would be greatly appreciated.
Code:
#include <iostream>
using namespace std;
struct Course {
Course();
Course(char* the_name, long the_courseID);
Course(const Course& the_object);
~ Course(){delete name;};
void operator = (const Course& the_object);
friend ostream & operator << (ostream & out, Course& r);
friend istream & operator >> (istream & in, Course& r);
char* name;
long courseID;
};
enum EStudentStatus {Fulltime, Partime, Exchange };
static char* studentstatusLabels[] = {"Fulltime",
"Partime",
"Exchange" };
enum EDepartment { Business, Engineering, Physics, ComputerScience };
static char* departmentLabels[] = {"Business",
"Engineering",
"Physics",
"ComputerScience" };
enum ERank {Instructor, AssociateProfessor, Professor, Dean };
static char* rankLabels[] = {"Instructor",
"AssociateProfessor",
"Professor",
"Dean" };
class Person {
public:
Person();
Person(string the_name, string the_address, unsigned long the_ID);
Person(const Person& the_object){};
~Person(){};
void operator = (const Person& the_object);
string get_name();
void set_name(string the_name);
string get_address();
void set_address(string the_address);
unsigned long get_ID();
void set_ID(unsigned long the_ID);
private:
string name;
string address; //office location for faculty
unsigned long ID;
};
const unsigned MAX_COURSES_FOR_STUDENT = 10;
class Student : virtual public Person {
public:
Student();
Student(const Student& the_object);
~Student(){};
void operator = (const Student& s);
friend istream & operator >> (istream & in, Student& r);
friend ostream & operator << (ostream & out, Student& r);
/*
EStudentStatus get_status();
void set_status(EStudentStatus the_status);
EDepartment get_department();
void set_department(EDepartment the_department);
Course* get_courses();
void set_courses(int courseCount);
int get_numCourses();
void set_numCourses(int courseCount);
*/
private:
EStudentStatus status;
EDepartment department;
Course* enrolled[MAX_COURSES_FOR_STUDENT];
int nCourses;
};
const unsigned MAX_COURSES_FOR_FACULTY = 5;
const unsigned MAX_GRADERS = 10;
Course :: Course()
{
name = NULL;
courseID = 0;
}
Course :: Course(char* the_name, long the_courseID)
{
name = the_name;
courseID = the_courseID;
}
Course :: Course(const Course& the_object)
{
}
void Course :: operator =(const Course& the_object)
{
if (this == &the_object) {
cout << "self" << endl;
}
name = the_object.name;
courseID = the_object.courseID;
return;
}
ostream & operator << (ostream & out, Course& r)
{
out << "Course Name: ";
out << r.name;
out << "\nCourse ID: ";
out << r.courseID;
return out;
}
istream & operator >> (istream & in, Course& r)
{
cout << "\nEnter Course Name: ";
in >> r.name;
cout << "\nEnter Course ID: ";
in >> r.courseID;
cout << "\nCourse Added.";
return in;
}
Person :: Person()
{
name = "empty";
address = "empty";
ID = 0;
}
Person :: Person(string the_name, string the_address, unsigned long the_ID)
{
name = the_name;
address = the_address;
ID = the_ID;
}
void Person :: operator = (const Person& the_object)
{
if (this == &the_object) {
cout << "self" << endl;
}
name = the_object.name;
address = the_object.address;
ID = the_object.ID;
return;
}
string Person :: get_name()
{
return name;
}
void Person :: set_name(string the_name)
{
name = the_name;
}
string Person :: get_address()
{
return address;
}
void Person :: set_address(string the_address)
{
address = the_address;
}
unsigned long Person :: get_ID()
{
return ID;
}
void Person :: set_ID(unsigned long the_ID)
{
ID = the_ID;
}
Student :: Student() : Person()
{
status = Fulltime;
department = Business;
nCourses = 0;
enrolled[MAX_COURSES_FOR_STUDENT];
}
istream & operator >> (istream & in, Student& r)
{
string s_name;
string s_address;
int s_ID;
int status_label;
int department_label;
Course Course1;
cout << "Enter student name: ";
getline(in, s_name);
r.set_name(s_name);
cout << "\nEnter student address: ";
getline(in, s_address);
r.set_address(s_address);
cout <<"\nEnter student ID: ";
in >> s_ID;
r.set_ID(s_ID);
cout <<"What is their status as a student? Enter 0 for Fulltime,"
<<"\n 1 for Partime, or 2 for Exchange.";
in >> status_label;
if (status_label == 0)
{
r.status = Fulltime;
}
else if (status_label == 1)
{
r.status = Partime;
}
else if (status_label == 2)
{
r.status = Exchange;
}
else
{
cout <<"Error, invalid student label." << endl;
}
cout <<"What department is the student in? Enter 0 for Business, 1 for Engineering,"
<<"\n 2 for Physics, and 3 for Computer Science." << endl;
in >> department_label;
if (department_label == 1)
{
r.department = Business;
}
else if (department_label == 2)
{
r.department = Engineering;
}
else if (department_label == 3)
{
r.department = Physics;
}
else if (department_label == 4)
{
r.department = ComputerScience;
}
else
{
cout << "Error, invalid department label." << endl;
}
cout << "How many courses are you taking?";
in >> r.nCourses;
cout << "List the courses you are taking. ";
for (int i = 0; i < r.nCourses; i++)
{
cin >> Course1;
r.enrolled[i] = &Course1;
}
cout << "Student Data entered." << endl;
return in;
}
ostream & operator << (ostream & out, Student& r)
{
out << "Student name is: " << r.get_name() << endl;
out << "Student address is: " << r.get_address() << endl;
out << "Student ID is: " << r.get_ID();
out << "Student status is: " << r.status << endl;
out << "Student department is: " << r.department << endl;
out << "Student is taking " << r.nCourses << " classes." << endl;
out << "Students class list is: " << endl;
for (int i = 0; i < MAX_COURSES_FOR_STUDENT; i++)
{
if (i < r.nCourses)
{
out << r.enrolled[i];
}
else
{
out << "Empty class slots." << endl;
}
}
return out;
}
int main()
{
Student test;
cin >> test;
cout << test;
system("pause");
return 0;
}