I am working on a school project and I am trying to add each student object "objStudent[5]" to an array of pointers "classRosterArray". When I try to initialize the pointer in the constructor "Roster". I receive the warning "Index 5 is out of valid index range". This is my first time learning C++ so I'm not sure what to do.
This is the Roster class where I created the array of pointers "classRosterArray"
//roster.h
#pragma once
#include <string>
using namespace std;
class Roster {
private:
string* classRosterArray[5];
public:
Roster();
void ParseStudentData();
void PrintAll();
};
The message "Index out of range" appears in the Roster::Roster constructor
//roster.cpp
#include "roster.h"
#include "student.h"
//studentData table
const string studentData[5] = {
"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",
"A5,Joe,Shmoe,jshmoe@google.com,34,20,30,40,SOFTWARE"
};
//Object for each student in the studentData table
Student objStudent[5];
Roster::Roster()
{
classRosterArray[5] = nullptr; //index out of range
classRosterArray[5] = new string[5]; //tried using this to solve issue taking stabs in the dark
}
//Function to parse each set of student data in the studentData table
void Roster::ParseStudentData() {
for (int i = 0; i < 5; i++) {
size_t rhs = studentData[i].find(",");
objStudent[i].SetStudentID(studentData[i].substr(0, rhs));
size_t lhs = rhs + 1;
rhs = studentData[i].find(",", lhs);
objStudent[i].SetFirstName(studentData[i].substr(lhs, rhs - lhs));
lhs = rhs + 1;
rhs = studentData[i].find(",", lhs);
objStudent[i].SetLastName(studentData[i].substr(lhs, rhs - lhs));
lhs = rhs + 1;
rhs = studentData[i].find(",", lhs);
objStudent[i].SetEmailAddress(studentData[i].substr(lhs, rhs - lhs));
lhs = rhs + 1;
rhs = studentData[i].find(",", lhs);
objStudent[i].SetAge(stoi(studentData[i].substr(lhs, rhs - lhs)));
}
}
void Roster::PrintAll() {
ParseStudentData(); //Call ParseStudentData function to parse studentData table
}