-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContacts.cpp
More file actions
256 lines (176 loc) · 3.99 KB
/
Contacts.cpp
File metadata and controls
256 lines (176 loc) · 3.99 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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "Contacts.h"
using namespace std;
char menu()
{
char choise;
cout << endl << string(25, '=') << endl;
cout << "WELCOME TO YOU'R CONTACTS" << endl;
cout << string(25, '=') << endl << endl;
cout << "Press 'A' to View all contacts" << endl;
cout << "Press 'B' to Add a new contact" << endl;
cout << "Press 'C' to Change(Edit) a contact" << endl;
cout << "Press 'D' to Delete a contact" << endl;
cout << "Press 'S' to Search for a contact" << endl;
cout << "Press 'E' to Exit" << endl << endl;
do
{
cout << "Enter your choice: " ;
cin >> choise;
choise = toupper(choise);
}while(choise != 'A' && choise != 'B' && choise != 'C' && choise != 'D'
&& choise != 'S' && choise != 'E');
system("CLS");
return choise;
}
char viewAll()
{
string temp;
ifstream fin;
fin.open("contacts.txt");
if(!fin)
{
ofstream fout;
fout.open("contacts.txt");
fout.close();
fin.open("contacts.txt");
}
if(!fin)
{
cout << "Error opening the file" << endl;
exit(1);
}
while (!fin.eof())
{
getline(fin, temp);
cout << temp << endl;
}
fin.close();
cout << "Press 'R' to go back to the main menu: ";
char back;
cin >> back;
back = toupper(back);
system("CLS");
return back;
}
char addNew(person people)
{
fstream fout("contacts.txt", ios::app);
cout << "Enter the name : ";
cin >> people.name;
cout << "Enter the phone : ";
cin >> people.number;
fout << "Name : " << people.name << endl;
fout << "phone number : " << people.number << endl;
cout << "Add successful" << endl;
fout.close();
cout << "Press 'R' to go back to the main menu: ";
char back;
cin >> back;
back = toupper(back);
system("CLS");
return back;
}
char edit(person people)
{
string find;
string line;
cout << "Enter the name to be updated : ";
cin >> find;
find = "Name : " + find; // when searching to add the name
cout << "Enter the new name : ";
cin >> people.name;
cout << "Enter the phone number : ";
cin >> people.number;
ifstream fin; //for reading records
fin.open("contacts.txt");
ofstream temp;
temp.open("temp.txt");
while (getline(fin, line))
{
if (line != find )
temp << line << endl;
else
{
getline(fin, line); // to skip the next line
temp << "Name : " << people.name << endl;
temp <<"phone number : " << people.number << endl;
}
}
fin.close();
temp.close();
remove("contacts.txt");
rename("temp.txt", "contacts.txt");
cout << "Press 'R' to go back to the main menu: ";
char back;
cin >> back;
back = toupper(back);
system("CLS");
return back;
}
char remove()
{
string find;
string line;
cout << "Enter the name to be deleted : ";
cin >> find;
find = "Name : " + find; // when searching to add the name
ifstream fin; //for reading records
fin.open("contacts.txt");
ofstream temp;
temp.open("temp.txt");
while (getline(fin, line))
{
if (line != find )
temp << line << endl;
else
{
getline(fin, line); // to skip the next line
}
}
fin.close();
temp.close();
remove("contacts.txt");
rename("temp.txt", "contacts.txt");
cout << "Press 'R' to go back to the main menu: ";
char back;
cin >> back;
back = toupper(back);
system("CLS");
return back;
}
char search()
{
string name, line ;
bool notfoud = true;
cout << "Enter the name you want to search : ";
cin >> name;
name = "Name : " + name;
ifstream fin;
fin.open("contacts.txt");
if (!fin)
{
cout << "The file is empty" << endl;
}
while (!fin.eof())
{
getline(fin , line);
if (name == line )
{
cout << name << endl;
getline(fin , line);
cout << line << endl;
notfoud = false;
}
}
if (notfoud)
cout << "The name could not be found check the spelling and capitalization" << endl;
cout << "Press 'R' to go back to the main menu: ";
char back;
cin >> back;
back = toupper(back);
system("CLS");
return back;
}