-
Notifications
You must be signed in to change notification settings - Fork 0
/
f24.cpp
107 lines (97 loc) · 2.65 KB
/
f24.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//----------------------------------------------------------------------------------------------------------------------------------
struct Employee {
int id;
string name;
string designation;
double salary;
};
//----------------------------------------------------------------------------------------------------------------------------------
void add_employee() {
ofstream outfile("employees.txt", ios::app);
Employee employee;
cout << "Enter employee details: ";
cout << "ID: ";
cin >> employee.id;
cout << "Name: ";
cin>>employee.name;
cout << "Designation: ";
cin>>employee.designation;
cout << "Salary: ";
cin >> employee.salary;
outfile.write((char *)&employee, sizeof(employee));
outfile.close();
}
//----------------------------------------------------------------------------------------------------------------------------------
void delete_employee() {
int id;
cout << "Enter the ID of the employee to delete: ";
cin >> id;
Employee employee;
ifstream infile("employees.txt");
ofstream outfile("temp.txt");
while (infile.read((char *)&employee, sizeof(employee))) {
if (employee.id != id) {
outfile.write((char *)&employee, sizeof(employee));
}
}
infile.close();
outfile.close();
remove("employees.txt");
rename("temp.txt", "employees.txt");
}
//----------------------------------------------------------------------------------------------------------------------------------
void display_employee() {
int id;
cout << "Enter the ID of the employee to display: ";
cin >> id;
Employee employee;
bool found = false;
ifstream infile("employees.txt");
while (infile.read((char *)&employee, sizeof(employee))) {
if (employee.id == id) {
cout << "Employee details: " << endl;
cout << "ID: " << employee.id << endl;
cout << "Name: " << employee.name << endl;
cout << "Designation: " << employee.designation << endl;
cout << "Salary: " << employee.salary << endl;
found = true;
break;
}
}
infile.close();
if (!found) {
cout << "Employee not found!" << endl;
}
}
//----------------------------------------------------------------------------------------------------------------------------------
int main() {
int choice;
do {
cout << "1. Add employee" << endl;
cout << "2. Delete employee" << endl;
cout << "3. Display employee" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
add_employee();
break;
case 2:
delete_employee();
break;
case 3:
display_employee();
break;
case 4:
cout<<"Existing";
default:
cout << "Invalid choice!" << endl;
}
} while (choice != 4);
return 0;
}