Explain errors on a c++ project

Lesson.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Lesson{
private:
    string lessonCode; //Code for the Lesson
    unsigned int lessonSemester; //Semester of the Lesson
    string lessonName; //Name of the Lesson
public:
    Lesson(string, string, unsigned int); //Constructor with Code, Name, Semester
    Lesson(); //Constructor
    string getLessonCode(); //Getter for the Leson's Code
    unsigned int getLessonSemester(); //Getter for the Lesson's Semester
    void setLessonCode(string); //Setter for the Lesson's Code
    void setLessonSemester(unsigned int); //Setter for the Lesson's Semester
    string getLessonName(); //Getter for the Lesson's Name
    void setLessonName(string); //Setter for the Lesson's Name
    ~Lesson(); //Destructor
  };


Student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
void Student::setCourseTable(float *coursetable){ //Set the courseTable property
   this->courseTable = coursetable;
}
void Student::setLessonTable(Lesson *LessonArray){ //Set the Lesson's Table
  this->lessonTable = LessonArray;
}
Lesson * Student::getLessonsTable(){ //Get the Lesson's Table
   return lessonTable;
}
void Student::operator+= (const Lesson &lesson){ //Overload the += operator
   unsigned int numberLessons = this->getNumberLessons();
   Lesson *lessonsArray = this->getLessonsTable();
   Lesson *tmpLessonArray;
  // if (lesson != NULL)
 //  {
     tmpLessonArray = new Lesson[numberLessons+1];
     for (int i = 0; i < numberLessons ; i++) tmpLessonArray[i] =  lessonsArray[i];
     tmpLessonArray[numberLessons] = lesson;
     delete[] lessonsArray;
     lessonsArray = tmpLessonArray;
     numberLessons++; 
     this->setLessonTable(tmpLessonArray);
     this->setNumberLessons(numberLessons);
 //  }
 }
Student::~Student(void) { //Destructor
      cout << "Destruct Object: " << this->fullName << endl;
      delete[] AM;
      delete courseTable;
      delete[] lessonTable;
}

void Student::InsertNewGrade(const float grade) //Insert a new Grade to the course table
  {    
      float *tmpTable = this->getCourseTable();
      float *tmpCourseTable = tmpTable;
      int tmpNumCourses = this->getNumCourses(); 
       // Below I demonstrate two different versions of the same functionality either with pointers either with dynamic allocations for the CourseTable table    
      /* 
      if ( (grade >= 5.0) && (grade <= 10.0) )
      {   
         for (int i = 0; i < tmpNumCourses; i++) ++tmpCourseTable;
         *tmpCourseTable = grade;          
         tmpNumCourses++;
      }
      */
      if ( (grade >= 5.0) && (grade <= 10.0) )
      {         
         float *tmpDynamiCoursesTable = new float[tmpNumCourses+1];        
         for (int i=0 ; i<tmpNumCourses; i++) tmpDynamiCoursesTable[i] = tmpTable[i];        
         tmpDynamiCoursesTable[tmpNumCourses] = grade;
         delete[] tmpTable;
         tmpTable = tmpDynamiCoursesTable;
         tmpNumCourses++;       
     }
     this->setNumCourses(tmpNumCourses);
     this->setCourseTable(tmpTable);
  }
 void Student::PrintStudentProperties(ostream &channel) //Print the Student properies
  {
      channel << " AM = " << this->getAM() << " FullName = " << this->getFullName() << " NumCourses = " << this->getNumCourses() << endl;
  }
  void Student::PrintCourseTable(){ //Print the Course Table
      float currentMO = 0.0;
      float *tmpcoursetable = this->getCourseTable();    
      unsigned int numcourses = this->getNumCourses(); 
      if (numcourses > 0){
       for (int i=0; i<numcourses; i++) {
          // cout << "Lesson's = " << i << " Grade = " << tmpcoursetable[i]; //this instruction make my program crash
           printf("Lesson's %d Grade = %f\n",i,tmpcoursetable[i]);
           currentMO += tmpcoursetable[i];
       }
        printf("The average of grades is = %f\n" ,(currentMO / (numcourses/1.0)));
       }
       else
       {
         cout << "No courses are succeeded yet!" << endl;
        }
     }
     void Student::PrintLessonsTable(Lesson *lessonTable, unsigned int numberLessons){ //Print the lesson table
      for (int i = 0; i < numberLessons; i++ )
       {
          this->PrintLessonProperties(cout, lessonTable[i]);
       }
     }
    void Student::PrintLessonProperties(ostream &channel, Lesson &lesson){ //Print the lesson's properties
      channel << "Lesson's Code = " << lesson.getLessonCode() << " Lesson's Semester = " << lesson.getLessonSemester() << " Lesson's Name = " << lesson.getLessonName() << endl;
      }


On main() when I do:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
int main(int argc, char** argv) {
  float tmpTable[] = {8.9, 6.5, 7.5};
  Student student1((char *)"30222", "student1");
  Student student2((char *)"30444", "student2", 2);
  Student student3((char *)"30555", "student3", 3, 3, tmpTable);
  Student student4 = student3;    
  cout << " ======Student1===== " << endl;   
  student1.PrintStudentProperties(cout); 
  student1.InsertNewGrade(8.9);
  student1.PrintStudentProperties(cout);
  student1.InsertNewGrade(6.5); 
  student1.PrintStudentProperties(cout);
  student1.InsertNewGrade(7.5); 
  student1.PrintStudentProperties(cout);
  student1.InsertNewGrade(4.5); 
  student1.PrintStudentProperties(cout);
  student1.InsertNewGrade(2.5); 
  student1.PrintStudentProperties(cout); 
  //float *ff = student1.getPassedCourseTable();
  //for (int i =0; i<5;i++) printf("%f\n",ff[i]);
  student1.PrintCourseTable();
  cout << " ======Student2===== " << endl;
  student2.PrintStudentProperties(cout); 
  student2.InsertNewGrade(8.9);
  student2.PrintStudentProperties(cout);
  student2.InsertNewGrade(6.5); 
  student2.PrintStudentProperties(cout);
  student2.InsertNewGrade(7.5); 
  student2.PrintStudentProperties(cout);
  student2.InsertNewGrade(4.5); 
  student2.PrintStudentProperties(cout);
  student2.InsertNewGrade(2.5); 
  student2.PrintStudentProperties(cout); 
  //float *ff = student2.getPassedCourseTable();
  //for (int i =0; i<5;i++) printf("%f\n",ff[i]);
  student2.PrintCourseTable();
  cout << " ======Student3===== " << endl;
  student3.PrintStudentProperties(cout);
  student3.PrintCourseTable();
  //float *ff = student3.getPassedCourseTable();
  //for (int i =0; i<5;i++) printf("%f\n",ff[i]);
  cout << " ======Student4===== " << endl;
  student4.PrintStudentProperties(cout);
  student4.PrintCourseTable();
  student4.InsertNewGrade(5.1);
  student4.PrintStudentProperties(cout);
  student4.InsertNewGrade(6.5);
  student4.PrintStudentProperties(cout);
  student4.InsertNewGrade(4.1);
  student4.PrintStudentProperties(cout);
  student4.PrintCourseTable();
  //float *ff = student4.getPassedCourseTable();
  //for (int i =0; i<5;i++) printf("%f\n",ff[i]);
  Lesson lesson1("AB1", "Math1", 1);
  Lesson lesson2("AB2", "Math2", 5);
  Lesson lesson3("AB3", "Math3", 4);
  Lesson lesson4("AB4", "Math4", 7);
  student1+=lesson1;
  student1+=lesson2;
  student1+=lesson3;
  student1+=lesson4;
  student2+=lesson1;
  student2+=lesson2;
  student2+=lesson3;      
  student2+=lesson4;
  student3+=lesson1;
  student3+=lesson2;
  student3+=lesson3;
  student3+=lesson4;
  student4+=lesson1;
  student4+=lesson2;
  student4+=lesson3;      
  student4+=lesson4;
  Lesson lesson5("AB5", "Math5", 6);
student1.PrintLessonsTable(student1.getLessonsTable(),student1.getNumberLessons());
student2.PrintLessonsTable(student2.getLessonsTable(),student2.getNumberLessons());
student3.PrintLessonsTable(student3.getLessonsTable(),student3.getNumberLessons());
student4.PrintLessonsTable(student4.getLessonsTable(),student4.getNumberLessons());
  }


1) When I run the InsertNewGrade() only with pointers and without the arrays (take a look on the code above) , although it runs perfectly I get Running Failed on Netbeans 8.0.

2) On PrintCourseTable() the cout does not run? I get Running failed… and it does not destruct all the Student objects.

3) On Student::operator+= I wan to check if ( lesson == null ) and I get error due to the overload == I make below.

Most of the times I get Running Succeeded but there are cases where I get Failed but always with the correct results. When I conitnue running the following instruction cout << "Lessons for Student2" << endl; and below but berfore the instruction cout << "Lessons for Student4" << endl; I always get Running Failed with the correct results and it cannot destruct the Student objects. Finally with the instruction cout << "Lessons for Student4" << endl; and below it does not print anything for Student4 object and always I get Running Failed.

Any suggestion?
Last edited on
Hello konstantinosdms,

Many.
but first I will start with you have posted code for "Lesson.h", but no ".cpp" file. Then you have code for "Student.cpp", but not header file. Then in "main" you do not include any "#includes". In short you have not provided a program that can be compiled and tested.
There is to much distance between us for me to be able to read your mind. I do not know what you are thinking or what you have left out.
In "main" you have int main(int argc, char** argv), but I have not found any place where you have used these parameters, so if you are not going to use them they do not need to be there int main() will work just as well.
After that it will take a little time to load up your code and fix it up to test it.

Andy
Topic archived. No new replies allowed.