I have a class Student and the dynamically created array of students:
class Student{
public:
  void create(){
    system("cls");
    cout << "First name: ";
    cin >> fn;
    cout << "Last name: ";
    cin >> ln;
    cout << "How many subjects? ";
    cin >> sub_number;
    subjects = new string[sub_number];
    for(int i = 0; i < sub_number; i++){
      cout << "Subject name: ";
      cin >> subject[i];
    }
  }
  //This just display the student with a bunch of couts
  friend ostream& operator<<(ostream&, const Student&);
  ~Student(){ delete[] subjects; }
private:
  string fn;
  string ln;
  string* subjects;
  int sub_number;
};
I need to increase the size of this array by 1 and add a new student to it.
When I tried to do it by simply:
void main(){
   int stu_number = 0;
   cout << "How many students? "
   cin >> stu_number;
   Student* students = new Student[stu_number];
   //creating/updating initial students using user input
   for(int i = 0; i < stu_number; i++){
     students[i].create();
   }
   //displaying all students
   for(int i = 0; i < stu_number; i++){
     cout << students[i];
   }
   //trying to add a student
   add_new_student(students, stu_number);
   //displaying all students again
   for(int i = 0; i < stu_number; i++){
     cout << students[i];
   }
}
And the add_new_student function:
void add_new_student(Student* students, int& stu_number){
  Student* temp = new Student[stu_number++];
  for (int i = 0; i < stu_number - 1; i++){
    temp[i] = students[i];
  }
  temp[stu_number - 1].create(); 
  delete[] students;
  students = temp;
}
I am getting memory violation errors and Unhandled exceptions: Access violation writing location 0xABABABAB inside the add_new_student function.
- What will be the correct way of doing this?
- Is Student class missing some important components?
I can't use std::vector or a std::list as this is a school assignment:)
 
     
     
     
    