-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContactManagement.cpp
222 lines (175 loc) · 5.13 KB
/
ContactManagement.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
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
#include<iostream>
#include<string>
#include<cstdlib> // for system()
using namespace std;
// To store the contact 's info
struct Contact
{
string name;
string email;
string phone;
};
void clearScreen(){
system("clear");
}
// function to display menu options
void displayMenu()
{
cout<<"\n..........Contact Mangement System........\n";
cout<<"1. Add a new contact \n";
cout<<"2. Display all contacts\n";
cout<<"3. Search for a contact\n";
cout<<"4. Update a contact\n";
cout<<"5. Delete a contact\n";
cout<<"6. Exit\n";
}
void addContact(Contact contacts[],int &num_contacts)
{ if(num_contacts<100)
{ Contact newContact;
cout<<"Enter name: ";
getline(cin,newContact.name);
cout<<"Enter phone number: ";
getline(cin,newContact.phone);
cout<<"Enter Email address: ";
getline(cin,newContact.email);
contacts[num_contacts++]=newContact;
cout<<"Contact has been added successfully.\n";
}
else cout<<"Maximum limit reached.\n";
}
void displayContacts(Contact contacts[],int num_contacts)
{
if(num_contacts==0)
{
cout<<"No contacts to display.\n";
}
else
{
cout<<"-----------All Contacts------------\n";
for(int i=0;i<num_contacts;i++)
{
cout<<"Name: "<<contacts[i].name<<"\n";
cout<<"phone: "<<contacts[i].phone<<"\n";
cout<<"Email: "<<contacts[i].email<<"\n";
cout<<"***********************************\n";
}
}
}
void searchContact(Contact contacts[],int num_contacts)
{
if(num_contacts==0)cout<<"No contacts to search.\n";
else
{
string searchName;
cout<<"Enter search Name: ";
getline(cin,searchName);
bool found=false;
for(int i=0;i<num_contacts;i++)
{
if(contacts[i].name==searchName)
{
cout<<"Enter new phone number.\n";
getline(cin,contacts[i].phone);
}
{
cout<<">>>>>>>>>>>>>>>>>>>>>>>>> Contact has been found >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n";
cout<<"Name: "<<contacts[i].name<<"\n";
cout<<"Phone:"<<contacts[i].phone<<"\n";
cout<<"Email: "<<contacts[i].email<<"\n";
found=true;
break;
}
}
if(!found)cout<<"Couldn't find the contact.\n";
}
}
//function to update a contact by name
void updateContact(Contact contacts[],int num_contacts)
{
if(num_contacts==0)cout<<"No contacts to update.\n";
else
{
string searchName;
cout<<"Enter name of contact to update.\n";
getline(cin,searchName);
bool found=false;
for(int i=0;i<num_contacts;i++)
{
if(contacts[i].name==searchName)
{
cout<<"Enter new phone number.\n";
getline(cin,contacts[i].phone);
cin.ignore();
cout<<"Enter new email.\n";
getline(cin,contacts[i].email);
found=true;
cout<<"Contact updated successfully.\n";
break;
}
}
if(!found)cout<<"Sorry,couldnt't find contact.\n";
}
}
void deleteContact(Contact *contacts,int&num_contacts)
{ if (num_contacts == 0)cout << "No contacts to delete.\n";
else
{
string searchName;
cout << "Enter name of contact to delete: ";
getline(cin, searchName);
bool found=false;
for( int i=0;i<num_contacts;i++)
{
if(searchName==contacts[i].name)
{
for(int j=i;j<num_contacts-1;j++)
contacts[j]=contacts[j+1]; // shifted contacts to the left and filled the gap
num_contacts--;
cout<<"Contact has been deleted succesfully.\n";
found=true;
break;
}
}
if (!found)
cout << "Contact not found.\n";
}
}
int main()
{ const int MAX_CONTACTS=100;
Contact contacts[MAX_CONTACTS];
int num_contacts=0;
int choice;
do
{
displayMenu();
cout<<"ENTER YOUR CHOICE: ";
cin>>choice;
cin.ignore();
clearScreen();
switch(choice)
{
case 1:
addContact(contacts,num_contacts);
break;
case 2:
displayContacts(contacts,num_contacts);
break;
case 3:
searchContact(contacts,num_contacts);
break;
case 4:
updateContact(contacts,num_contacts);
break;
case 5:
deleteContact(contacts,num_contacts);
break;
case 6:
break;
default:
cout<<"Invalid choice. Please try again.\n";
break;
}
}while(choice!=6);
clearScreen();
return 0;
}