-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhonebook.cpp
137 lines (130 loc) · 4.2 KB
/
Phonebook.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
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
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstring>
using namespace std;
class Dict {
char name[20], ph[15];
public:
void getdata() {
cout << "\nEnter the phone details:";
cout << "\nName: ";
cin >> name;
cout << "\nphone Number: ";
cin >> ph;
}
void putdata() {
cout << "\n" << left << setw(20) << name;
cout << left << setw(15) << ph;
}
char *getName() {
return name;
}
char *gettel() {
return ph;
}
void update(char *nam, char *ph) {
strcpy(name, nam);
strcpy(ph, ph);
}
};
int main() {
char ph[15], name[20], ch;
int cho, flag = 0, cnt = 0;
fstream f;
Dict t;
f.open("ph.txt", ios::out | ios::in | ios::binary);
do {
cout << "\n=====================MENU=====================";
cout << "\n 1. Create record";
cout << "\n 2. Display all records";
cout << "\n 3. Search by name";
cout << "\n 4. Search by phone number";
cout << "\n 5. Update phone number";
cout << "\n 6. Exit";
cout << "\nEnter your choice: ";
cin >> cho;
switch (cho) {
case 1:
t.getdata();
f.write((char *) &t, sizeof(t));
break;
case 2:
cout << "\nAll phone records are:";
cout << "\n Name \t\tTelephoneNumber";
f.seekg(0, ios::beg);
for (f.read((char *) &t, sizeof(t)); !f.eof(); f.read((char *) &t, sizeof(t)))
t.putdata();
f.clear();
break;
case 3:
cout << "\nEnter name for searching: ";
cin >> name;
f.seekg(0, ios::beg);
for (f.read((char *) &t, sizeof(t)); !f.eof(); f.read((char *) &t, sizeof(t))) {
if (strcmp(name, t.getName()) == 0) {
cout << "\nAll phone records are:";
cout << "\n Name \t\tTelephoneNumber";
t.putdata();
flag = 1;
}
}
f.clear();
if (flag == 0)
cout << "\nRecord not found.";
break;
case 4:
cout << "\nEnter telephone number for searching: ";
cin >> ph;
f.seekg(0, ios::beg);
flag = 0;
for (f.read((char *) &t, sizeof(t)); !f.eof(); f.read((char *) &t, sizeof(t))) {
if (strcmp(ph, t.gettel()) == 0) {
cout << "\nAll phone records are:";
cout << "\n Name \t\tTelephoneNumber";
t.putdata();
flag = 1;
}
}
f.clear();
if (flag == 0)
cout << "\nRecord not found.";
break;
case 5:
cout << "\nEnter name for updating: ";
cin >> name;
flag = 0;
f.seekg(0, ios::beg);
for (f.read((char *) &t, sizeof(t)); !f.eof(); f.read((char *) &t, sizeof(t))) {
cnt++;
if (strcmp(name, t.getName()) == 0) {
flag = 1;
break;
}
}
f.clear();
if (flag == 0)
cout << "\nRecord not found.";
else {
int loc = (cnt - 1) * sizeof(t);
if (f.eof())
f.clear();
cout << "\nEnter new phone number: ";
cin >> ph;
f.seekp(loc);
t.update(name, ph);
f.flush();
f.write((char *) &t, sizeof(t));
}
break;
case 6:
return 0;
default:
cout << "\nEnter valid choice.";
break;
}
cout << "\nDo you want to continue? (y/n): ";
cin >> ch;
} while (ch == 'Y' || ch == 'y');
return 0;
}