-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12F.cpp
More file actions
217 lines (205 loc) · 4.63 KB
/
Copy path12F.cpp
File metadata and controls
217 lines (205 loc) · 4.63 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
class Employee
{
private:
int code;
char name[20];
float salary;
public:
void read();
void display();
int getEmpCode() { return code; }
int getSalary() { return salary; }
void updateSalary(float s) { salary = s; }
};
fstream file;
void deleteExistingFile()
{
remove("EMPLOYEE.DAT");
}
void appendToFile()
{
Employee x;
x.read();
file.open("EMPLOYEE.DAT", ios::binary | ios::app);
if (!file)
{
cout << "ERROR IN CREATING FILE\n";
return;
}
file.write(reinterpret_cast<char *>(&x), sizeof(x));
file.close();
cout << "Record added successfully.\n";
}
void displayAll()
{
Employee x;
file.open("EMPLOYEE.DAT", ios::binary | ios::in);
if (!file)
{
cout << "ERROR IN OPENING FILE\n";
return;
}
while (file.read(reinterpret_cast<char *>(&x), sizeof(x)))
{
if (x.getSalary() >= 10000 && x.getSalary() <= 20000)
{
x.display();
}
}
file.close();
}
void searchForRecord()
{
Employee x;
int c;
int isFound = 0;
cout << "Enter employee code: ";
cin >> c;
file.open("EMPLOYEE.DAT", ios::binary | ios::in);
if (!file)
{
cout << "ERROR IN OPENING FILE\n";
return;
}
while (file.read(reinterpret_cast<char *>(&x), sizeof(x)))
{
if (x.getEmpCode() == c)
{
cout << "RECORD FOUND\n";
x.display();
isFound = 1;
break;
}
}
if (isFound == 0)
{
cout << "Record not found!!!\n";
}
file.close();
}
void increaseSalary()
{
Employee x;
int c;
int isFound = 0;
float sal;
cout << "Enter employee code: ";
cin >> c;
file.open("EMPLOYEE.DAT", ios::binary | ios::in);
if (!file)
{
cout << "ERROR IN OPENING FILE\n";
return;
}
while (file.read(reinterpret_cast<char *>(&x), sizeof(x)))
{
if (x.getEmpCode() == c)
{
cout << "Salary hike? ";
cin >> sal;
x.updateSalary(x.getSalary() + sal);
isFound = 1;
break;
}
}
if (isFound == 0)
{
cout << "Record not found!!!\n";
}
file.close();
cout << "Salary updated successfully." << endl;
}
void insertRecord()
{
Employee x, newEmp;
newEmp.read();
fstream fin;
file.open("EMPLOYEE.DAT", ios::binary | ios::in);
fin.open("TEMP.DAT", ios::binary | ios::out);
if (!file)
{
cout << "Error in opening EMPLOYEE.DAT file!!!\n";
return;
}
if (!fin)
{
cout << "Error in opening TEMP.DAT file!!!\n";
return;
}
while (file.read(reinterpret_cast<char *>(&x), sizeof(x)))
{
if (x.getEmpCode() > newEmp.getEmpCode())
{
fin.write(reinterpret_cast<char *>(&newEmp), sizeof(newEmp));
newEmp = x;
}
else
{
fin.write(reinterpret_cast<char *>(&x), sizeof(x));
}
}
fin.write(reinterpret_cast<char *>(&newEmp), sizeof(newEmp));
fin.close();
file.close();
rename("TEMP.DAT", "EMPLOYEE.DAT");
remove("TEMP.DAT");
cout << "Record inserted successfully." << endl;
}
void Employee::read()
{
cout << "Enter employee code: ";
cin >> code;
cout << "Enter name: ";
cin.ignore(1);
cin.getline(name, 20);
cout << "Enter salary: ";
cin >> salary;
}
void Employee::display()
{
cout << code << " " << name << "\t" << salary << endl;
}
int main()
{
char ch;
deleteExistingFile();
do
{
int n;
cout << "ENTER CHOICE\n"
<< "1. ADD AN EMPLOYEE\n"
<< "2. DISPLAY\n"
<< "3. SEARCH\n"
<< "4. INCREASE SALARY\n"
<< "5. INSERT RECORD\n";
cout << "Make a choice: ";
cin >> n;
switch (n)
{
case 1:
appendToFile();
break;
case 2:
displayAll();
break;
case 3:
searchForRecord();
break;
case 4:
increaseSalary();
break;
case 5:
insertRecord();
break;
default:
cout << "Invalid Choice\n";
}
cout << "Do you want to continue? (Y/N): ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
return 0;
}