当我运行以下代码时:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int SIZE = 50;
//PROTOTYPES
int loadArrays(string[], int[]);
double computeAverage (int, int[]);
void printEmployees(string[], int[], int);
int addEmployee(string[], int[], int);
int validateYears();
int removeEmployee (string[], int[], int);
//MAIN
int main()
{
string names[SIZE];
int experience[SIZE];
int noRecords;
cout << "Initial array load:\n\n"<<endl;
noRecords=loadArrays(names,experience);
printEmployees(names,experience,noRecords);
cout << "An employee added:\n\n"<<endl;
noRecords=addEmployee(names,experience,noRecords);
printEmployees(names,experience,noRecords);
cout << "Attempted to add same employee:\n\n"<<endl;
noRecords=addEmployee(names,experience,noRecords);
printEmployees(names,experience,noRecords);
cout << "Employee deleted:\n\n"<<endl;
noRecords=removeEmployee(names,experience,noRecords);
printEmployees(names,experience,noRecords);
cout << "Attempted to delete same employee load\n\n"<<endl;
noRecords=removeEmployee(names,experience,noRecords);
printEmployees(names,experience,noRecords);
cout << "Program ended successfully"<<endl;
system("type employees.dat");
system("pause");
}
//Functions
int loadArrays(string empName[], int expYears[])
{
int length=0;
int ctr=0;
ifstream fin("employees.dat");
if (!fin.is_open())
{
cout << "File cannot be opened" << endl;
system("pause");
exit(-1);
}
for (; length < SIZE; length++)
{
getline(fin,empName[length]);
fin >> expYears[length];
if (fin.eof())
break;
fin.ignore(80, '\n');
}
cout << ("Successfully loaded arrays!\n\n");
return length;
}
double computeAverage (int length, int expYears[])
{
int totalExp;
double average;
for (int ctr=0; ctr < length; ctr++)
{
totalExp+=expYears[ctr];
}
return average = totalExp/length;
}
void printEmployees(string names[], int experience[], int noRecords)
{
for (int ctr=0; ctr < noRecords; ctr++)
cout << "Employee's name: " <<names[ctr] << endl << "Years of experience " << experience[ctr] << endl << endl;
cout <<"There are " << noRecords << " records." << endl << endl;
}
int addEmployee(string name[], int experience[], int noRecords)
{
string newEmployee;
int newExperience;
int index;
int ctr;
if (noRecords == SIZE)
cout << "There is no room for more records!";
else
{
cout << "Input a new employee's name: ";
getline(cin,newEmployee);
newExperience = validateYears();
}
for (index=0; index < noRecords; index++)
{
if (name[index]>=newEmployee)
break;
if (name[index]==newEmployee)
cout << "That name is already in our files";
else
{
for (ctr=noRecords; ctr > index; ctr--)
{
name[ctr] = name[ctr-1];
experience[ctr] = experience[ctr-1];
}
name[index] = newEmployee;
experience[index] = newExperience;
noRecords++;
}
}
return noRecords;
}
int validateYears()
{
int newExperience;
cout << endl << "Input the employees experience: ";
cin >> newExperience;
while (newExperience < 0 || newExperience > 100 || cin.fail())
{
cout << "Please enter a valid number between one and one-hundred to represent the number of years of experience: ";
cin.ignore(80, '\n');
cin >> newExperience;
}
return newExperience;
}
int removeEmployee (string names[], int experience[], int noRecords)
{
int ctr;
string employeeToRemove;
cout << "Enter the name of the employee you wish to remove (In 'J. Doe' format): ";
getline (cin, employeeToRemove);
for (ctr = 0; ctr < noRecords; ctr++)
{
if (employeeToRemove == names[ctr])
break;
else if (ctr == noRecords)
cout << "That name is not in our records";
}
for (ctr; ctr < noRecords; ctr++)
{
names[ctr] = names[ctr+1];
experience[ctr] = experience[ctr++];
}
noRecords--;
return noRecords;
}
在输入员工姓名和员工多年的经验之后,它给了我
Employee.exe中0x5a52ca58(msvcr100d.dll)的未处理异常: 0xC0000005:访问冲突写入位置0xf18be2b7。
我试过移动变量,但我没有看到问题。 提前谢谢!
答案 0 :(得分:0)
for (index; index < noRecords; index++)
{
if (name[index]>=newEmployee)
break;
}
if (name[index]==newEmployee)
cout << "That name is already in our files";
else
{
for (ctr=noRecords; ctr > index; ctr--)
{
name[ctr] = name[ctr-1];
experience[ctr] = experience[ctr-1];
}
name[index] = newEmployee;
experience[index] = newExperience;
noRecords++;
}
return noRecords;
}
寻找插入点的for循环不仅仅是搜索插入点。 移动了一些支撑架和tadaaa~
感谢您的帮助,我将阅读一些调试教程。