能用C++写小型通讯录设计吗
时间: 2024-03-21 16:38:45 浏览: 16
当然可以用C++来实现一个小型通讯录设计。以下是一个简单的示例代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct Contact {
string name;
string phone;
string email;
};
int main() {
vector<Contact> contacts;
int choice;
cout << "Welcome to the contact book!" << endl;
while (true) {
cout << "Please select an option:" << endl;
cout << "1. Add a contact" << endl;
cout << "2. List all contacts" << endl;
cout << "3. Quit" << endl;
cin >> choice;
if (choice == 1) {
Contact contact;
cout << "Enter the name: ";
cin >> contact.name;
cout << "Enter the phone number: ";
cin >> contact.phone;
cout << "Enter the email address: ";
cin >> contact.email;
contacts.push_back(contact);
cout << "Contact added successfully!" << endl;
} else if (choice == 2) {
if (contacts.empty()) {
cout << "No contacts found." << endl;
} else {
cout << "Contacts:" << endl;
for (const auto& contact : contacts) {
cout << "Name: " << contact.name << endl;
cout << "Phone: " << contact.phone << endl;
cout << "Email: " << contact.email << endl;
cout << endl;
}
}
} else if (choice == 3) {
cout << "Goodbye!" << endl;
break;
} else {
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}
```
这个程序使用了结构体来表示一个联系人的信息,使用了vector来存储所有的联系人。程序提供了三个选项:添加联系人、列出所有联系人和退出程序。用户可以根据自己的需要进行操作。