Question: why does my program crash? #include #include #include using namespace std; struct Employee { int id; string firstName; int age; int salary; int departmentId; }

why does my program crash?
#include
#include
#include
using namespace std;
struct Employee {
int id;
string firstName;
int age;
int salary;
int departmentId;
};
void addEmployee(){
Employee emp;
cout << "Enter employee id: "<< endl;
cin >> emp.id;
cout << "Enter employee first name: "<< endl;
cin >> emp.firstName;
cout << "Enter employee department id: "<< endl;
cin >> emp.departmentId;
cout << "Enter employee age: "<< endl;
cin >> emp.age;
cout << "Enter employee salary: "<< endl;
cin >> emp.salary;
ofstream outFile("employees.txt", ios::app);
if (outFile.is_open()){
outFile << emp.id << emp.firstName << emp.departmentId << emp.age << emp.salary << endl;
outFile.close();
cout << "Employee data was saved to a file." << endl;
cout << endl;
}
else {
cout << "Error opening file!" << endl;
}
}
void searchEmployee(){
int searchId;
cout << "Enter employee id you want to search:" << endl;
cout <<"***********************************"<< endl;
cin >> searchId;
ifstream inFile("employees.txt");
if (inFile.is_open()){
Employee emp;
bool found = false;
while (inFile >> emp.id >> emp.firstName >> emp.departmentId
>> emp.age >> emp.salary){
if (emp.id == searchId){
cout << "Employee id is: "<< emp.id << endl;
cout << "Employee name is: "<< emp.firstName << endl;
cout << "Employee department id is: "<< emp.departmentId << endl;
cout << "Employee age is "<< emp.age << endl;
cout << "Employee salary is: "<< emp.salary << endl;
found = true;
break;
}
}
if (!found){
cout << "Employee with this id is not found." << endl;
}
inFile.close();
}
else {
cout << "Error opening file!" << endl;
}
}
int main(){
int choice;
do {
cout <<"***********************************"<< endl;
cout <<"1. To add employee data press 1."<< endl;
cout <<"2. To search for an employee press 2."<< endl;
cout <<"3. Quit the program press 3."<< endl;
cout << "Please enter your choice." << endl;
cin >> choice;
cout << "Please enter 1,2, or 3."<< endl;
cout << "Thank you for using the program!" << endl;
switch (choice){
case 1:
addEmployee();
break;
case 2:
searchEmployee();
break;
case 3:
break;
}
return 0;
}
while (choice !=3);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!