Question
In this code right here I have a function called showEmployeeKeys, that show what keys an employee has, hower, when I try to input a
In this code right here I have a function called showEmployeeKeys, that show what keys an employee has, hower, when I try to input a valid name into the program it starts an infinite loop showing the main menu infinitely, this doesn't happen when I input an invalid name.
The employee's information is taking from a file with the format 2 Ya Hoo 3 AHC102 AHC200 AHC111 Michael Lee 2 AHC303 AHC200 The first number represents the number of employees, then there is the name of one employee, then the number of keys and the name of each key. Also, in main in option 4, If I enter a valid name it will skip the second cin altogether, but if I don't give a valid name it does not skip it, the same thing happens with option 5 but that's not complete yet so I con't want to focus on that one. I need some help I have spent several hours trying to fix the issues specially the first one, I've re-written the function, the menu, etc but I can't seem to find the problem. How can I fix this? #include
#include
#include
using namespace std;
struct Employee{
string name;
int nKeysPossessed;
string keys[5];
};
bool reader(string input_filename, Employee employees[], int& nEmployees){
ifstream fin(input_filename);
if (!fin.is_open()){
return false;
}
else {
fin>>nEmployees; //get number of employees from file
fin.ignore();
for(int i=0; i getline(fin, employees[i].name); fin>>employees[i].nKeysPossessed; fin.ignore(); for (int j=0; j fin>>employees[i].keys[j]; fin.ignore(); } } return true; } } void writer(string output_filename, Employee employees[], int nEmployees){ ofstream fout(output_filename); if (output_filename==""){ for (int i = 0; i < nEmployees; i++) { cout << "Name: " << employees[i].name << endl; cout << "Keys possessed: "; for (int j = 0; j < employees[i].nKeysPossessed; j++) { cout << employees[i].keys[j] << " "; } cout << endl; } cout< } else{ for (int i = 0; i < nEmployees; i++) { fout << "Name: " << employees[i].name << endl; fout << "Keys possessed: "; for (int j = 0; j < employees[i].nKeysPossessed; j++) { fout << employees[i].keys[j] << " "; } fout << endl; } } } void showEmployeeKeys(Employee employees[], int nEmployees, string emp_name){ bool employee_found = false; // flag to check if the employee is found for (int i=0; i if (employees[i].name == emp_name){ employee_found = true; cout << employees[i].name << " possesses the following keys: "; for (int j=0; j cout << employees[i].keys[j] << " "; } cout << endl; break; // exit the loop once the employee is found } } if (!employee_found){ cout << "Cannot find the specified employee!" << endl; } } bool addKeyForEmployee(Employee employees[], int nEmployees, string emp_name, string newKey){ for (int i=0; i if (employees[i].name != emp_name){ cout<<"Cannot find the specified employee!"< return false; } else if (employees[i].name == emp_name){ if (employees[i].nKeysPossessed == 5){ cout<<"This employee already has 5 keys!"< return false; } else if (employees[i].nKeysPossessed < 5){ for (int j=0; j if (newKey == employees[i].keys[j]){ cout<< "This employee already has this key!"< return false; } else{ employees[i].keys[employees[i].nKeysPossessed] = newKey; employees[i].nKeysPossessed++; return true; } } } } } } bool returnAKey(Employee employees[], int nEmployees, string emp_name, string returnKey){ return false; } int main(){ Employee employees[999]; string input_filename, output_filename, emp_name, newKey,speckey, returnKey; int option=0; int nEmployees; bool input; //opens file and calls function cout<<"Please enter key file name to start: "; cin>>input_filename; input = reader(input_filename, employees, nEmployees); //menu if (input == true){ while (option != 7){ cout<<"Please select from the following options: "< cout<<" 1. show all employees and their keys"< cout<<" 2. show the keys an employee possesses"< cout<<" 3. show which employees possess a specific key"< cout<<" 4. add a key to an employee"< cout<<" 5. return a key by an employee"< cout<<" 6. save the current key status"< cout<<" 7. exit the program"< cin >> option; //show employees info (done) if(option== 1){ writer(output_filename, employees, nEmployees); } //show keys from employee (infinite loop still) else if (option==2){ cout<<"Please enter employee's name: "< cin>>emp_name; showEmployeeKeys(employees, nEmployees, emp_name); } //which employees have a specific key (done) else if(option==3){ string posses[nEmployees]; bool found; found=false; int count=0; cout<<"Please enter a key: "; cin>>speckey; for (int i=0; i for (int j=0; j if (speckey==employees[i].keys[j]) { posses[i]=employees[i].name; found=true; count++; break; } } } if (!found) { cout << "No one possesses this key." << endl;} else { for (int i=0; i cout< } cout<<"possess this key."< } cout< } //Adding a key to employee (buffer problem still) else if(option==4){ cout<<"Please enter employee's name: "< cin>>emp_name; cin.ignore(); cout<<"Please enter a new key: "< cin>>newKey; cin.ignore(); addKeyForEmployee(employees, nEmployees, emp_name, newKey); } //deleting key from employee (buffer problem still) else if (option==5){ cout<<"Please enter employee's name: "< cin>>emp_name; cin.ignore(); cout<<"Please enter the returned key: "< cin>>returnKey; returnAKey(employees, nEmployees, emp_name, returnKey); } //save status (done) else if (option==6){ cout<<"Please enter output file name: "< cin>>output_filename; cin.ignore(); writer(output_filename, employees, nEmployees); } //exit program and save status (done) else if (option==7){ output_filename= "keys_updated.txt"; writer(output_filename, employees, nEmployees); cout<<"Thank you for using the system! Goodbye!"< } else if(option>7 || option<1){ cout<<"Not a valid option. Please try again."< } } }else cout<<"File not found, exiting the program..."< return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started