Program using classes - keeps crashing

This is a discussion on Program using classes - keeps crashing within the C++ Programming forums, part of the General Programming Boards category; Hello, I wrote a program using classes to get myself more familar with OOP. I got my program error free, ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Program using classes - keeps crashing

    Hello,
    I wrote a program using classes to get myself more familar with OOP. I got my program error free, but whenever I run the code, the program crashes. Any hints on why would be greatly appreciated. Thanks.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class courseTranscript
    {
    private:
    	string dept;
    	string courseNum;
    	string courseTitle;
    	int courseCredit;
    	char grade;
    public:
    	void setDept(string d);
    	string getDept(void);
    	void setCourseNum(string n);
    	string getCourseNum(void);
    	void setCourseTitle(string t);
    	string getCourseTitle(void);
    	void setCourseCredit(int c);
    	int getCourseCredit(void);
    	void setGrade(char g);
    	char getGrade(void);
        
    };
    
    class student
    {
    private:
    	string firstName;
    	string lastName;
    	int studentID;
    	string ssn;
    	string dob;
    	string major;
    	courseTranscript transcripts[50];
    	int numCourseTaken;
    public:
    	student();
        student(string fn, string ln, int id, string ssn);
        void setFirstName(string fn);
    	string getFirstName(void);
    	void setLastName(string ln);
    	string getLastName(void);
    	void setStudentID(int id);
    	int getStudentID(void);
    	void setSSN(string ssn);
    	string getSSN(void);
    	void setDOB(string dob);
    	string getDOB(void);
    	void setMajor(string m);
    	string getMajor(void);
    	void setNumCourseTaken(int nct);
    	int getNumCourseTaken(void);
    	void AddToTranscripts(string d, string n, string t, int c, char g);
    	void printTranscript(void);
    	void printCourses(void);
    	double getGPA();
    	courseTranscript getTranscript(int index);
    };
    
    class athlete : public student
    {
    private:
    	string sports;
    public:
    	athlete();
    	athlete(string fn, string ln, int id, string ssn, string s);
    	void setSports(string s);
    	string getSports(void);
        void printTranscript(void);
    };
    
    int main(void)
    {
    
    	student newstudent;
    	student anotherstudent("John", "Smith", 1234, "123-45-6789");
    	athlete stu3("Jim", "Smith", 3456, "345-67-8901", "Basketball");
    
    	newstudent.setFirstName("Jane");
    	newstudent.setLastName("Smith");
    	newstudent.setSSN("234-56-7890");
    	newstudent.setStudentID(2345);
    	newstudent.setDOB("01/01/1980");
    	newstudent.setMajor("Computer Science");
    	newstudent.AddToTranscripts("ITS", "2591", "C++ Programming I", 4, 'A');
    	newstudent.AddToTranscripts("ITS", "1040", "System Analysis and Designs", 3, 'B');
    	newstudent.AddToTranscripts("ITS", "1110", "Programming Logic, Designs, and Development", 3, 'A');
    	stu3.AddToTranscripts("ITS", "2591", "C++ Programming I", 4, 'A');
    	stu3.AddToTranscripts("ITS", "1040", "System Analysis and Designs", 3, 'B');
    
    	newstudent.printTranscript();
    	anotherstudent.printTranscript();
    	stu3.printTranscript();
    
        return 0;
    }
    // couseTranscript functions
    void courseTranscript::setDept(string d)
    {
    	dept = d;
    }
    string courseTranscript::getDept(void)
    {
    	return dept;
    }
    void courseTranscript::setCourseNum(string n)
    {
    	courseNum = n;
    }
    string courseTranscript::getCourseNum(void)
    {
    	return courseNum;
    }
    void courseTranscript::setCourseTitle(string t)
    {
    	courseTitle = t;
    }
    string courseTranscript::getCourseTitle(void)
    {
    	return courseTitle;
    }
    void courseTranscript::setCourseCredit(int c)
    {
    	courseCredit = c;
    }
    int courseTranscript::getCourseCredit(void)
    {
    	return courseCredit;
    }
    void courseTranscript::setGrade(char g)
    {
    	grade = g;
    }
    char courseTranscript::getGrade(void)
    {
    	return grade;
    }
    
    // Student functions
    student::student()
    {
    	firstName = "Jon";
    	lastName = "Doe";
    	studentID = 0000;
    	ssn = "0000-00-0000";
    	dob = "00/00/0000";
    	major = "Undefined";
    	numCourseTaken = 0;
    }
    student::student(string fn, string ln, int id, string ssn)
    {
    	firstName = fn;
    	lastName = ln;
    	studentID = id;
    	ssn = ssn;
    }
    void student::setFirstName(string fn) 
    {
    	firstName = fn;
    }
    string student::getFirstName(void) {
    	return firstName;
    }
    void student::setLastName(string ln) 
    {
    	lastName = ln;
    }
    string student::getLastName(void)
    {
    	return lastName;
    }
    void student::setStudentID(int id)
    {
    	studentID = id;
    }
    int student::getStudentID(void)
    {
    	return studentID;
    }
    void student::setSSN(string ssn)
    {
    	ssn = ssn;
    }
    string student::getSSN(void)
    {
    	return ssn;
    }
    void student::setDOB(string dob)
    {
    	dob = dob;
    }
    string student::getDOB(void)
    {
    	return dob;
    }
    void student::setMajor(string m)
    {
    	major = m;
    }
    string student::getMajor(void)
    {
    	return major;
    }
    void student::setNumCourseTaken(int nct)
    {
    	numCourseTaken = nct;
    }
    int student::getNumCourseTaken(void)
    {
    	return numCourseTaken;
    }
    void student::AddToTranscripts(string d, string n, string t, int c, char g)
    {
        transcripts[numCourseTaken].setDept(d);
    	transcripts[numCourseTaken].setCourseNum(n);
    	transcripts[numCourseTaken].setCourseTitle(t);
    	transcripts[numCourseTaken].setCourseCredit(c);
    	transcripts[numCourseTaken].setGrade(g);
    
    	numCourseTaken++;
    }
    void student::printTranscript()
    {
    	cout << "Name     : " << getFirstName() << " " << getLastName() << endl;
    	cout << "SSN      : " << getSSN() << endl;
    	cout << "DOB      : " << getDOB() << endl;
    	cout << "Major    : " << getMajor() << endl;
    	cout << "Number of courses taken     : " << getNumCourseTaken() << endl << endl;
    
        printCourses();
    	
    }
    double student::getGPA()
    {
    	double gpa;
    	double grade;
    	double totalpoints = 0;
    	double totalcredits = 0;
    
    
    
    	for (int i = 0; i < numCourseTaken; i++)
    	{
             switch (transcripts[numCourseTaken].getGrade())
    		 {
                 case 'A':
    	         case 'a': grade = 4.0;
    		     break;
    	         case  'B':
    	         case 'b': grade = 3.0;
    		     break;
    	         case 'C':
    	         case 'c': grade = 2.0;
    		     break;
    	         case 'D':
    	         case 'd': grade = 1.0;
    		     break;
    	         default: grade = 0.0;
    		 }
    		 totalcredits = totalcredits + transcripts[numCourseTaken].getCourseCredit();
    		 totalpoints = totalpoints + (grade * transcripts[numCourseTaken].getCourseCredit());
    	}
        gpa = totalpoints / (totalcredits * numCourseTaken);
    
    	return gpa;
    
    }
    void student::printCourses(void)
    {
    	for (int i = 0; i < numCourseTaken; i++)
    	{
            cout << "Dept:     " << transcripts[numCourseTaken].getDept() << endl;
    	    cout << "Course Num:     " << transcripts[numCourseTaken].getCourseNum() << endl;
    	    cout << "Course Title:   " << transcripts[numCourseTaken].getCourseTitle() << endl;
    	    cout << "Course Credit:  " << transcripts[numCourseTaken].getCourseCredit() << endl;
    	    cout << "Grade:    " << transcripts[numCourseTaken].getGrade() << endl;
    	    cout << "GPA:      " << getGPA();
    	}
    }
    courseTranscript student::getTranscript(int index)
    {
        return transcripts[index];
    }
    
    // Athlete functions
    void athlete::setSports(string s)
    {
    	sports = s;
    }
    string athlete::getSports(void)
    {
    	return sports;
    }
    athlete::athlete() 
    {
    	sports = "Sports Undefined";
    }
    athlete::athlete(string fn, string ln, int id, string ssn, string s) : student(fn, ln, id, ssn)
    {
    	sports = s;
    
    	getTranscript(getNumCourseTaken()).setDept("WFS");
    	getTranscript(getNumCourseTaken()).setCourseNum("1050");
    	getTranscript(getNumCourseTaken()).setCourseTitle("Coed Team Sports and Activities");
    	getTranscript(getNumCourseTaken()).setCourseCredit(1);
    	getTranscript(getNumCourseTaken()).setGrade('A');
    }
    void athlete::printTranscript()
    {
        cout << "Name     : " << getFirstName() << " " << getLastName() << endl;
    	cout << "SSN      : " << getSSN() << endl;
    	cout << "DOB      : " << getDOB() << endl;
    	cout << "Major    : " << getMajor() << endl;
    	cout << "Number of courses taken     : " << getNumCourseTaken() << endl;
    	cout << "STUDENT ATHLETE" << endl << endl; 
    
       printCourses();
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    So far, I know it's in the line
    Code:
    	athlete stu3("Jim", "Smith", 3456, "345-67-8901", "Basketball");
    in main.

    and the error is occuring on this part of your code
    Code:
    courseTranscript student::getTranscript(int index)
    {
        return transcripts[index];
    }
    The value for index is showing -834572346 or something like that to show that index was never initialized to the proper value.
    Last edited by FoodDude; 09-16-2005 at 03:40 PM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    oh, int c++ you can initialize like:
    Code:
    int yourvalue = 0;

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    student::student(string fn, string ln, int id, string ssn)
    {
    	firstName = fn;
    	lastName = ln;
    	studentID = id;
    	ssn = ssn;
    }
    You never initialize numCourseTaken in this constructor. Also, I don't see how that last line can work. You probably want to use a different argument name, or use: this->ssn = ssn;

    Just how long exactly did you code without pressing the compile button? Seems to me you should have been able to narrow down the problem pretty quick if you weren't writing too much code at a time.
    "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

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Ah, thanks for helping me recognize the problem. I actually never compiled the code. Maybe that's bad practice-I don't know. I just typed it all up, compiled it and eventually got rid of all the errors, and finally ran it.

    Now the only problem is getting the outputs correct. Once I intalized numCourseTaken in the constructor, I get werid (garbage) values for certain things.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Program Classes Question
    By racerday182 in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2008, 02:03 AM
  2. Program crashing, Need help!
    By Obsidian_179 in forum C Programming
    Replies: 3
    Last Post: 06-19-2008, 05:26 PM
  3. Replies: 2
    Last Post: 06-18-2004, 08:08 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21