title: Exception thrown: read access violation. **_Right_data** was 0x4. C++
body: <p>I tried everything and looked everywhere but I keep getting 
  <strong>Exception thrown: read access violation.
  **_Right_data</strong> was 0x4.**</p>
  
  <p>What is wrong with the code? I am not very good with C++ and don't like it at all but working on this school project and trying to figure out why I get the exception. Any help is appreciated </p>
  
  <pre><code>#include "Roster.h"
  #include "Student.h"
  #include "NetworkStudent.h"
  #include "SecurityStudent.h"
  #include "SoftwareStudent.h"
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include&lt;vector&gt;
  #include&lt;sstream&gt;
  #include&lt;regex&gt;
  
  // Separates Data on basis of ,
  template &lt;class Container&gt;
  void splitString(const std::string&amp; str, Container&amp; cont)
  {
      char delim = ',';
      std::stringstream ss(str);
      std::string token;
      while (std::getline(ss, token, delim)) {
          cont.push_back(token);
      }
  }
  
  // Checks and returns if email is valid or not
  bool Email(std::string email)
  {
      const std::regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
      return regex_match(email, pattern);
  }
  
  // Main Function
  int main()
  {
      Roster classRoster;
      // Data Array
      const std::string studentData[] = { "A1,John,Smith,John1989@gm ail.com,20,30,35,40,SECURITY", 
          "A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK", 
          "A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE", 
          "A4,Erin,Black,Erin.black@comcast.net,22,50,58,40,SECURITY", 
  };
  
      for (unsigned int i = 0;i &lt; 5;i++)
      {
          std::vector&lt;std::string&gt; words;
          std::string s = studentData[i];
          // Splits Input
          splitString(s, words);
  
          // if Student belongs to Security
          if (words.at(8) == "SECURITY")
          {
              int a[3] = { stoi(words.at(5)),stoi(words.at(6)),stoi(words.at(7)) };
              classRoster.classRosterArray[i] = new SecurityStudent(words.at(0), words.at(1), words.at(2), words.at(3), stoi(words.at(4)), a, SECURITY);
          }
  
          // IF Student Belongs to Software
          else if (words.at(8) == "SOFTWARE")
          {
              int a[3] = { stoi(words.at(5)),stoi(words.at(6)),stoi(words.at(7)) };
              classRoster.classRosterArray[i]=new SoftwareStudent(words.at(0), words.at(1), words.at(2), words.at(3), stoi(words.at(4)), a, SOFTWARE);
          }
  
          // If Student Belongs to Network
          else if (words.at(8) == "NETWORK")
          {
              int a[3] = { stoi(words.at(5)),stoi(words.at(6)),stoi(words.at(7)) };
              classRoster.classRosterArray[i] = new NetworkStudent(words.at(0), words.at(1), words.at(2), words.at(3), stoi(words.at(4)), a, NETWORK);
          }
      }
      std::cout &lt;&lt; "\n---------Print All Student's Data------------\n";
      classRoster.printAll();
      std::cout &lt;&lt; "\n---------------------------------------------\n";
      std::cout &lt;&lt; "Invalid Emails\n";
      classRoster.printInvalidEmails();
      std::cout &lt;&lt; "\n--------------Days in Course------------------\n";
      classRoster.printDaysInCourse("A2"); 
      std::cout &lt;&lt; "\n--------------By Degree Program------------------";
      classRoster.printByDegreeProgram(3);
      std::cout &lt;&lt; "\n--------------Removes A3------------------\n";
      classRoster.remove("A3"); 
      classRoster.remove("A3");
      return 0;
  }
  
  // Adds Student to Roster
  void Roster::add(std::string studentID, std::string firstName, std::string lastName, std::string emailAddress, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, degree d)
  {
      if (d == SOFTWARE)
      {
          delete classRosterArray[4];
          int a[3] = { daysInCourse1,daysInCourse2,daysInCourse3 };
          classRosterArray[4] = new SoftwareStudent(studentID, firstName, lastName, emailAddress, age,a, d);
      }
      else if (d == NETWORK) 
      {
          delete classRosterArray[4];
          int a[3] = { daysInCourse1,daysInCourse2,daysInCourse3 };
          classRosterArray[4] = new NetworkStudent(studentID, firstName, lastName, emailAddress, age, a, d);
      }
  
      else if (d == SECURITY)
      {
          delete classRosterArray[4];
          int a[3] = { daysInCourse1,daysInCourse2,daysInCourse3 };
          classRosterArray[4] = new SecurityStudent(studentID, firstName, lastName, emailAddress, age, a, d);
      }
  }
  
  // Removes Student
  void Roster::remove(std::string studentID)
  {
      for (unsigned i = 0;i &lt; 5;i++)
      {
          if (classRosterArray[i]-&gt;getStudentID() == studentID){
              std::cout &lt;&lt; "STUDENT REMOVED " &lt;&lt; classRosterArray[i]-&gt;getStudentID()&lt;&lt;"\n";
              classRosterArray[i] = NULL;
          }
      }
      std::cout &lt;&lt; "Student ID Does not Exist \n";
  }
  
  // Prints All Data Of Array
  void Roster::printAll()
  {
      for (unsigned i = 0;i &lt; 5;i++)
      {
          std::cout &lt;&lt; i + 1 &lt;&lt; "\t";
          classRosterArray[i]-&gt;print();
          std::cout &lt;&lt; "\n";
      }
  }
  
  // Prints Average Days in Course for a specific Student
  void Roster::printDaysInCourse(std::string studentID)
  {
      int sum = 0;
      for (unsigned i = 0;i &lt; 5;i++)
      {
          if (classRosterArray[i]-&gt;getStudentID() == studentID)
          {
              int *p = classRosterArray[i]-&gt;getStudentDaysofCourses();
              for (unsigned int j = 0;j &lt; 3;j++)
                  sum += p[j];
              delete[]p;
              break;
          }
      }
      std::cout &lt;&lt; sum / 3.0;
  }
  
  // Prints Invalid Emails
  void Roster::printInvalidEmails()
  {
      for (unsigned i = 0;i &lt; 5;i++)
      {
          std::string email = classRosterArray[i]-&gt;getStudentEmail();
          if (!Email(email))
          {
              std::cout &lt;&lt; classRosterArray[i]-&gt;getStudentEmail();
              //classRosterArray[i]-&gt;print();
              std::cout &lt;&lt; "\n";
          }
  
      }
  }
  
  
  // Prints By Degree Program
  void Roster::printByDegreeProgram(int degreeProgram)
  {
      for (unsigned i = 0;i &lt; 5;i++)
      {
          if (classRosterArray[i]-&gt;getDegreeProgram()==degreeProgram)
          classRosterArray[i]-&gt;print();
          std::cout &lt;&lt; "\n";
      }
  }
  
  // Destructor
  Roster::~Roster()
  {
      for (unsigned i = 0; i &lt; 5; i++)
      {
          if (classRosterArray[i]!=NULL)
              delete classRosterArray[i];
      }
  }
  
  </code></pre>