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 outFileemployeestxt ios::app;
if outFileisopen
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 inFileemployeestxt;
if inFileisopen
Employee emp;
bool found false;
while inFile emp.id emp.firstName emp.departmentId
emp.age emp.salary
if empid 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 To add employee data press endl;
cout To search for an employee press endl;
cout Quit the program press endl;
cout "Please enter your choice." endl;
cin choice;
cout "Please enter or endl;
cout "Thank you for using the program!" endl;
switch choice
case :
addEmployee;
break;
case :
searchEmployee;
break;
case :
break;
return ;
while choice ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
