-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelephone.cpp
More file actions
300 lines (242 loc) · 6.11 KB
/
Copy pathtelephone.cpp
File metadata and controls
300 lines (242 loc) · 6.11 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//***************************************************************
// HEADER FILE USED IN PROJECT
//****************************************************************
#include <iostream>
#include<conio.h>
#include<fstream>
#include<string.h>
using namespace std;
class phonebook{
char name[30];
char phno[20];
char city[20];
char pin[10];
char email[40];
public:
void getdata(){
cout<<"\nEnter Name : ";
gets(name);
cout<<"Enter Phone No. : ";
gets(phno);
int x=strlen(phno);
while(x!=10)
{
cout<<"enter valid phone no.:";
gets(phno);
x=strlen(phno);
}
cout<<"Enter city : ";
gets(city);
cout<<"Enter pin No. : ";
gets(pin);
int y=strlen(pin);
while(y!=6)
{
cout<<"enter valid pin no.:";
gets(pin);
y=strlen(pin);
}
cout<<"Enter email ID : ";
gets(email);
}
void showdata(){
cout<<"\n" ;
cout<<"Name: "<<name<<endl;
cout<<"Phoneno: "<<phno<<endl;
cout<<"City: "<<city<<endl;
cout<<"Pin: "<<pin<<endl;
cout<<"Email Id: "<<email<<endl;
}
char *getname() { return name; }
char *getcity() { return city; }
}; //class ends here
char nm[30];
// global declaration for stream object, object
fstream fp;
phonebook record;
// function to write in file
void write_record()
{
fp.open("telephone.dat",ios::out|ios::app);
record.getdata();
fp.write((char*)&record,sizeof(record));
fp.close();
cout<<"\n\nThe Record Has Been Created ";
getch();
}
// function to read all records from file
void display_all()
{ int n=0;
system("cls");
cout<<"\n\n\n\t\tDISPLAY ALL RECORDS !!!\n\n";
fp.open("telephone.dat",ios::in);
while(fp.read((char*)&record,sizeof(record)))
{ n++;
record.showdata();
cout<<"\n\n====================================\n";
getch();
}
cout<<"Total NO. of Records are : "<<n;
fp.close();
getch();
}
// function to read specific record from file
void display_record()
{ char nm[30];
system("cls");
cout<<"\n\n\tPlease Enter The name of the person ";
gets(nm);
int flag=0;
fp.open("telephone.dat",ios::in);
while(fp.read((char*)&record,sizeof(record)))
{
if(strcmp(record.getname(),nm)==0)
{
cout<<"The Record You Searched For is :"<<endl;
record.showdata();
flag=1;
}
}
fp.close();
if(flag==0)
cout<<"\n\nRecord not exist";
getch();
}
// DISPLAY A PARTICULAR CITY RECORD
void display_recordcity()
{ char ct[50]; int no=0;
system("cls");
cout<<"\n\n\tPlease Enter The name of the city ";
gets(ct);
int flag=0;
fp.open("telephone.dat",ios::in);
while(fp.read((char*)&record,sizeof(record)))
{
if(strcmp(record.getcity(),ct)==0)
{ no++;
record.showdata();
flag=1;
}
}
cout<<"\n\n Total Records "<<no;
fp.close();
if(flag==0)
cout<<"\n\nRecord does not exist";
getch();
}
// function to modify record of file
void modify()
{
char nm[30];int found=0;
system("cls");
cout<<"\n\n\tTo Modify ";
cout<<"\n\n\tPlease Enter The name of the person\n";
cout<<"\n \twhose details you want to update :";
gets(nm);
fp.open("telephone.dat",ios::in|ios::out);
while(fp.read((char*)&record,sizeof(record)) && found==0)
{
if(strcmp(record.getname(),nm)==0)
{
record.showdata();
cout<<"\nPlease Enter The New Details"<<endl;
record.getdata();
int pos=-1*sizeof(record);
fp.seekp(pos,ios::cur);
fp.write((char*)&record,sizeof(record));
cout<<"\n\n\t Record Updated";
found=1;
record.showdata();
}
}
fp.close();
if(found==0)
cout<<"\n\n Record Not Found ";
getch();
}
// function to delete record of file
void delete_record()
{ int found =0;
char confirm='n';
char nm[10];
system("cls");
cout<<"\n\n\n\tDelete Record";
cout<<"\n\nPlease Enter The name of the person You Want To Delete : ";
cin>>nm;
fp.open("telephone.dat",ios::in|ios::out);
fstream fp2;
fp2.open("Temp.dat",ios::out);
fp.seekg(0,ios::beg);
while(fp.read((char*)&record,sizeof(record)))
{
if(strcmp(record.getname(),nm)==0)
{
record.showdata();
found =1;
cout<<"Are You Sure You Want To Delete The Record ";
cin>>confirm;
if(confirm=='n')
{ cout<<"Record Still There ";
fp2.write((char*)&record,sizeof(record));
} }
else
fp2.write((char*)&record,sizeof(record));
}
if(confirm!='n')
cout<<"Record Deleted..";
fp2.close();
fp.close();
remove("telephone.dat");
rename("Temp.dat","telephone.dat");
if(found==0)
cout<<"\n\n Record Not Found ";
getch();
}
// THE MAIN FUNCTION OF PROGRAM
void menu()
{
char ch;
do
{
system("cls");
cout<<endl<<endl<<endl<<endl;
cout<<"\n\n\t \t \t \t \t \tTELEPHONE DIRECTORY ";
cout<<"\n\n\t \t \t \t \t \tMAIN MENU";
cout<<"\n\n\t \t \t \t \t \t1.CREATE RECORD";
cout<<"\n\n\t \t \t \t \t \t2.DISPLAY ALL RECORD";
cout<<"\n\n\t \t \t \t \t \t3.SEARCH A PARTICULAR RECORD";
cout<<"\n\n\t \t \t \t \t \t4.SEARCH RECORDS OF PARTICULAR CITY";
cout<<"\n\n\t \t \t \t \t \t5.MODIFY A RECORD";
cout<<"\n\n\t \t \t \t \t \t6.DELETE RECORD";
cout<<"\n\n\t \t \t \t \t \t7.EXIT";
cout<<"\n\n\t \t \t \t \t \tPlease Select Your Option (1-7) ";
ch=getch();
switch(ch)
{
case '1': system("cls");
write_record();
break;
case '2':display_all();
break;
case '3': display_record();
break;
case '4' :display_recordcity();
break;
case '5':
modify();
break;
case '6': delete_record();
break;
case '7':
exit(0);
default:cout<<"ENTER VALID CHOICE";
}
}
while(ch!='7');
}
int main()
{
menu();
return 0;
}
// END OF PROJECT