我有一个程序分配,以将数据加载到类名册数组。它加载正常,但是当我尝试通过printAll
函数打印数组内容时,出现错误:
在Test.exe中的0x00C2F213处引发异常:0xC0000005:访问冲突读取位置0xCCCCCCCC
数据将加载所有五个学生的数据。 printAll
函数使用for
循环(在for
循环内是对虚拟打印函数的调用)来打印每条记录。但是它会将加载的最后一个学生记录(即使我将索引计数器初始化为0)打印到数组,然后崩溃。不知何故,我的索引/指针指向数组中的最后一条记录,而不是第一个。
这是我的代码:
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();
这是我的printAll
函数:
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;
这是另一个cpp文件中的print()
函数:
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;
}
这是我的输出:
正在加载临时表...
临时表已加载...
正在加载课程名册...
正在加载classRoster记录#:A1
正在加载classRoster记录#:A2
加载classRoster记录#:A3
加载classRoster记录#:A4
正在加载classRoster记录#:A5
类名册已加载...
打印班级名册...
数组项数:5
A5名:William姓:Byrd电子邮件地址:wbyrd7@wgu.edu年龄:57天课程:{32,11,23}学位课程:网络
ClassRoster行#:0