【发布时间】:2020-04-03 10:14:38
【问题描述】:
我有两个文本文件,Load function 将数据从两个文本文件传输到单个结构 (Employee emp[length]),其 const 长度为 2001。这是因为文本文件中有 2000 个员工详细信息。
将数据加载到 struct 后,我想使用 Select 函数搜索和显示员工数据。
系统将提示用户选择将用于搜索的员工属性和关键字。但是,我意识到我不能返回 struct(emp[i]) 或字符串值 (emp[i].empId)。它会提示一个错误说
访问冲突读取位置0x00D2C000
但是,我可以使用 cout 显示字符串值(emp[i].empId)。
我可以知道为什么我可以cout 字符串值但不能返回吗?
提前感谢您的帮助,并为我的英语不好感到抱歉。
const int length = 2001;
struct Employee {
string empId;
string dOB;
string height;
string weight;
string yrOfWork;
string salary;
string allowance;
string name;
string country;
string designation;
string gender;
string lvlOfEdu;
};
Employee emp[length];
void Load();
Employee Select(int k, string s, int c);
int main() {
bool quit = false;
int option;
while (quit != true) { //loop the program unless 7 is chosen
Load();
cout << "1. Add" << endl; //
cout << "2. Delete" << endl;
cout << "3. Select" << endl;
cout << "4. Advanced Search" << endl;
cout << "5. Standard Deviation" << endl;
cout << "6. Average" << endl;
cout << "7. Quit" << endl;
cout << "Please key in an option: ";
cin >> option;
system("cls"); //to refresh the screen
switch (option) {
case 3: {
int search;
string key;
cout << "1. Employee ID" << endl;
cout << "2. Date of Birth" << endl;
cout << "3. Height" << endl;
cout << "4. Weight" << endl;
cout << "5. Years of Working" << endl;
cout << "6. Basic Salary" << endl;
cout << "7. Allowance" << endl;
cout << "8. Employee Name" << endl;
cout << "9. Country" << endl;
cout << "10. Designation" << endl;
cout << "11. Gender" << endl;
cout << "12. Level of Education" << endl;
cout << "Select By: ";
cin >> search;
cout << "Enter keyword: ";
cin >> key;
for (int i = 0; i < length; i++) {
cout << Select(search, key, i).empId;
}
system("pause");
system("cls");
break;
}
}
}
}
Employee Select(int s, string k, int c) {
int result;
int i = c;
switch(s) {
case 1:
result = emp[i].empId.find(k);
if (result >= 0) {
return emp[i];
}
break;
}
}
void Load() {
ifstream inFigures;
inFigures.open("profiles_figures.txt");
ifstream inWords;
inWords.open("profiles_words.txt");
if (inFigures.is_open()) {
int i = 0;
while (!inFigures.eof()) {
inFigures >> emp[i].empId;
inFigures.ignore();
inFigures >> emp[i].dOB;
inFigures.ignore();
inFigures >> emp[i].height;
inFigures.ignore();
inFigures >> emp[i].weight;
inFigures.ignore();
inFigures >> emp[i].yrOfWork;
inFigures.ignore();
inFigures >> emp[i].salary;
inFigures.ignore();
inFigures >> emp[i].allowance;
inFigures.ignore();
i++;
}
}
//inFigures.close();
if (inWords.is_open()) {
int i = 0;
while (!inWords.eof()) {
getline(inWords, emp[i].name);
getline(inWords, emp[i].country);
getline(inWords, emp[i].designation);
inWords >> emp[i].gender;
inWords.ignore();
inWords >> emp[i].lvlOfEdu;
inWords.ignore();
i++;
}
}
//inWords.close();
}
【问题讨论】:
-
1) 请提供您使用的输入。如果
s != 1或result < 0,您认为您的Select函数会返回什么? 2)另外,“_with a const length of 2001。这是因为文本文件中有2000个员工详细信息”的推理。没有意义。如果一个文件中正好有 2000 条员工记录,2000的数组就足够了。可能出现问题的原因是您在读取文件时可能会遇到inWords.eof ()you are using。 -
数组不需要额外的元素。我认为您将“C 字符串”数组与一般数组混淆了。