用c++写一个小型通讯录管理系统
时间: 2023-08-04 13:10:40 浏览: 132
好的,以下是一个简单的 C++ 通讯录管理系统的示例代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Contact {
string name;
string phone;
string email;
};
vector<Contact> contacts;
void addContact() {
Contact c;
cout << "Enter name: ";
getline(cin, c.name);
cout << "Enter phone number: ";
getline(cin, c.phone);
cout << "Enter email: ";
getline(cin, c.email);
contacts.push_back(c);
cout << "Contact added successfully!" << endl;
}
void showContacts() {
if (contacts.empty()) {
cout << "No contacts found!" << endl;
return;
}
cout << "Name\tPhone\tEmail" << endl;
for (const auto& c : contacts) {
cout << c.name << "\t" << c.phone << "\t" << c.email << endl;
}
}
int main() {
int choice;
while (true) {
cout << "1. Add contact" << endl;
cout << "2. Show contacts" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore(); // Ignore the newline character
switch (choice) {
case 1:
addContact();
break;
case 2:
showContacts();
break;
case 3:
cout << "Exiting program..." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
}
```
这个程序使用了一个 `Contact` 结构体来存储每个联系人的姓名、电话和电子邮件地址。用户可以选择添加联系人或查看当前存储的所有联系人。
阅读全文