-
Notifications
You must be signed in to change notification settings - Fork 3
/
exercise3.cpp
57 lines (46 loc) · 1.9 KB
/
exercise3.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
#include <iostream>
#include <vector>
#include <algorithm>
struct Contact {
std::string name;
std::string address;
std::string email;
std::string phoneNumber;
};
std::vector<Contact> phonebook;
void addContact(const std::string& name, const std::string& address, const std::string& email, const std::string& phoneNumber) {
phonebook.push_back({name, address, email, phoneNumber});
}
void updateContact(const std::string& name, const std::string& newName, const std::string& newAddress, const std::string& newEmail, const std::string& newPhoneNumber) {
auto it = std::find_if(phonebook.begin(), phonebook.end(),
[&name](const Contact& c) { return c.name == name; });
if (it != phonebook.end()) {
it->name = newName;
it->address = newAddress;
it->email = newEmail;
it->phoneNumber = newPhoneNumber;
} else {
std::cout << "Contact not found" << std::endl;
}
}
void removeContact(const std::string& name) {
phonebook.erase(std::remove_if(phonebook.begin(), phonebook.end(),
[&name](const Contact& c) { return c.name == name; }), phonebook.end());
}
void searchContact(const std::string& name) {
auto it = std::find_if(phonebook.begin(), phonebook.end(),
[&name](const Contact& c) { return c.name == name; });
if (it != phonebook.end()) {
std::cout << "Name: " << it->name << ", Address: " << it->address << ", Email: " << it->email << ", Phone Number: " << it->phoneNumber << std::endl;
} else {
std::cout << "Contact not found" << std::endl;
}
}
int main() {
addContact("John Doe", "123 Main St", "johndoe@example.com", "123-456-7890");
addContact("Jane Doe", "456 Elm St", "janedoe@example.com", "098-765-4321");
searchContact("John Doe");
updateContact("John Doe", "John Doe", "456 Main St", "johndoe@example.com", "123-456-7890");
searchContact("John Doe");
return 0;
}