I have a program assignment to load data to a class roster array. It loads fine but when I try to print the array contents, via the printAll
function, I get the error:
Exception thrown at 0x00C2F213 in Test.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC
The data loads all five student's data. The printAll
function uses a for
loop (within the for
loop is a call to a virtual print function) that prints each record. But it prints the last student record loaded (even though I initialize my index counter to 0) to the array and then crashes. Somehow my index/pointer is pointing to the last record in the array and not the first one.
Here's my code:
int main() {
string rowText;
int commaPos1 = 0;
int commaPos2 = 2;
//int daysInCourse[3];
string StudentDataArray[5][9];
const string studentData[] =
{
"A1,John,Smith,John1989@gm ail.com,20,30,35,40,NETWORK", //SECURITY
"A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK", //NETWORK
"A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,NETWORK", //SOFTWARE
"A4,Erin,Black,Erin.black@comcast.net, 22,50,58,40,NETWORK", //SECURITY
"A5,William,Byrd ,wbyrd7@wgu.edu,57,32,11,23,NETWORK", //SOFTWARE
};
//Loading Students to a multi-column array prior to loading it into class roster.
cout << "Loading temporary table..." << endl;
int row, rowLength;
int column;
for (row = 0;row < 5;++row) { //for each row:
rowText = (studentData[row]); // read the row into variable, get its character length
rowLength = rowText.size(); // and set the column position counters to zero
commaPos1 = 0;
commaPos2 = 0;
for (column = 0;column < 9; ++column) { //for each column:
if (column != 8) { //Column is not the last column (last column needs to be processed differently due to final comma)
commaPos2 = studentData[row].find(",", commaPos2);
StudentDataArray[row][column] = studentData[row].substr(commaPos1, commaPos2 - commaPos1);
++commaPos2;
commaPos1 = commaPos2;
}
else {
StudentDataArray[row][column] = studentData[row].substr(commaPos1, rowLength - commaPos1);
}
}
}
cout << "Temporary table loaded...";
//load Class Roster from array
Roster classRoster;
Degree DegreeProgramEnum = NETWORKING;
cout << "Loading class Roster..." << endl;
for (row = 0; row < 5; row++) {
/*if (StudentDataArray[row][8] == "NETWORK") {
DegreeProgramEnum = NETWORKING;
//cout << "Student is is the networking degree program." << endl;
}
else if (StudentDataArray[row][8] == "SOFTWARE") {
DegreeProgramEnum = SOFTWARE;
}
else if (StudentDataArray[row][8] == "SECURITY") {
DegreeProgramEnum = SECURITY;
}
else {
cout << "ERROR: Degree Program is not NETWORKING, SOFTWARE OR SECURITY: " << StudentDataArray[row][8];
}*/
classRoster.add(StudentDataArray[row][0], //Student ID
StudentDataArray[row][1], //First Name
StudentDataArray[row][2], //Last Name
StudentDataArray[row][3], //Email
stoi(StudentDataArray[row][4]), //Age
stoi(StudentDataArray[row][5]), //DaysInCourse1
stoi(StudentDataArray[row][6]), //DaysInCourse2
stoi(StudentDataArray[row][7]), //DaysInCourse3
DegreeProgramEnum); //Enumerated degree program: SECURITY = 0, NETWORK = 1, SOFTWARE = 2
cout << "loading classRoster Record #: " << StudentDataArray[row][0] << endl;
}
cout << "Class Roster loaded..." << endl;
//print student data array
classRoster.printAll();
Here's my printAll
function:
void Roster::printAll() {
//cout << "C867 Scripting and Programming - Applications" << endl;
//cout << "C++" << endl;
//cout << "William Byrd (ID# 000941206)" << endl;
//cout << endl << endl;
cout << "Printing Class Roster..." << endl;
int e = 0;
int arrayitems = sizeof(classRosterArray) / sizeof(classRosterArray[e]);
cout << "number of array items: " << arrayitems << endl;
for (int i = 0; i < arrayitems; i++) {
if (classRosterArray[i] != nullptr) {
classRosterArray[i]->print();
cout << "ClassRoster row #: " << i << endl;
}
}
cout << "Class Roster Printed..." << '\n' << endl;
and here's my print()
function from another cpp file:
void Student::print()
{
string degree;
if (getsDegreeProgram() == 0)
degree = "SECURITY";
else if (getsDegreeProgram() == 1)
degree = "NETWORK";
else // getsDegreeProgram() == 2: SOFTWARE
degree = "SOFTWARE";
cout << getSID() << "\t"
<< "First Name: " << getsFName() << "\t"
<< "Last Name: " << getsLName() << "\t"
<< "Email Address: " << getsEmail() << "\t"
<< "Age: " << getsAge() << "\t" << " "
<< "Days In Course: { " << getsDIC()[0] << ", " << getsDIC()[1] << ", " << getsDIC()[2] << " }\t"
<< "Degree Program: " << degree << endl;
}
Here's my output:
Loading temporary table...
Temporary table loaded...
Loading class Roster...
loading classRoster Record #: A1
loading classRoster Record #: A2
loading classRoster Record #: A3
loading classRoster Record #: A4
loading classRoster Record #: A5
Class Roster loaded...
Printing Class Roster...
number of array items: 5
A5 First Name: William Last Name: Byrd Email Address: wbyrd7@wgu.edu Age: 57 Days In Course: { 32, 11, 23 } Degree Program: NETWORK
ClassRoster row #: 0
User contributions licensed under CC BY-SA 3.0