-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathF_24.cpp
More file actions
237 lines (203 loc) · 5.2 KB
/
Copy pathF_24.cpp
File metadata and controls
237 lines (203 loc) · 5.2 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*Company maintains employee information as employee ID, name, designation and salary.
Allow user to add, delete information of employee. Display information of particular
employee. If employee does not exist an appropriate message is displayed. If it is, then
the system displays the employee details. Use index sequential file to maintain the data.*/
#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
//Employee class Declaration
class Employee{
private:
int code;
char name[20];
float salary;
public:
void read();
void display();
//will return employee code
int getEmpCode() { return code;}
//will return employee salary
int getSalary() { return salary;}
//will update employee salary
void updateSalary(float s) { salary=s;}
};
//Read employee record
void Employee::read(){
cout<<"Enter employee code: ";
cin>>code;
cout<<"Enter name: ";
cin.ignore(1);
cin.getline(name,20);
cout<<"Enter salary: ";
cin>>salary;
}
//Display employee record
void Employee::display()
{
cout<<code<<" "<<name<<"\t"<<salary<<endl;
}
//global declaration
fstream file;
//Will delete file when program is being executed
//because we are create file in append mode
void deleteExistingFile(){
remove("EMPLOYEE.DAT");
}
//function to append record into file
void appendToFille(){
Employee x;
//Read employee record from user
x.read();
file.open("EMPLOYEE.DAT",ios::binary|ios::app);
if(!file){
cout<<"ERROR IN CREATING FILE\n";
return;
}
//write into file
file.write((char*)&x,sizeof(x));
file.close();
cout<<"Record added sucessfully.\n";
}
void displayAll(){
Employee x;
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
if(!file){
cout<<"ERROR IN OPENING FILE \n";
return;
}
while(file){
if(file.read((char*)&x,sizeof(x)))
if(x.getSalary()>=10000 && x.getSalary()<=20000)
x.display();
}
file.close();
}
void searchForRecord(){
//read employee id
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){
if(file.read((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();
}
//Function to increase salary
void increaseSalary(){
//read employee id
Employee x;
int c;
int isFound=0;
float sal;
cout<<"enter employee code \n";
cin>>c;
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
if(!file){
cout<<"ERROR IN OPENING FILE \n";
return;
}
while(file){
if(file.read((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;
}
//Insert record by assuming that records are in
//ascending order
void insertRecord(){
//read employee record
Employee x;
Employee newEmp;
//Read record to insert
newEmp.read();
fstream fin;
//read file in input mode
file.open("EMPLOYEE.DAT",ios::binary|ios::in);
//open file in write mode
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){
if(file.read((char*)&x,sizeof(x))){
if(x.getEmpCode()>newEmp.getEmpCode()){
fin.write((char*)&newEmp, sizeof(newEmp));
}
//no need to use else
fin.write((char*)&x, sizeof(x));
}
}
fin.close();
file.close();
rename("TEMP.DAT","EMPLOYEE.DAT");
remove("TEMP.DAT");
cout<<"Record inserted successfully."<<endl;
}
int main()
{
char ch;
//if required then only remove the file
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:
appendToFille();
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 ? : ";
cin>>ch;
}while(ch=='Y'||ch=='y');
return 0;
}