I have two text files and the Load function
transfers the data from both text files to a single struct (Employee emp[length]
) with a const length of 2001. This is because there are 2000 employee details in the text file.
After loading the data into struct, I wanted to search and display employee data using the Select function.
The user will be prompted to choose which employee attribute and keyword that is going to be used for searching. However, I realize that I cannot return a struct(emp[i])
or a string value(emp[i].empId
). It will prompt out an error saying
access violation reading location 0x00D2C000
However, I am able to display the string value(emp[i].empId
) using cout
.
May I know why can I cout
the string value but not return it?
Thank you for your help in advance and sorry for my poor English.
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();
}