C++通讯录管理系统代码示例
C++通讯录管理系统代码示例:
include
include
include
include
using namespace std;
// 定义联系人结构体
struct Contact {
string name;
string phone;
string email;
};
// 添加联系人
void addContact(vector& contacts) {
Contact newContact;
cout << "请输入姓名: ";
cin >> newContact.name;
cout << "请输入电话号码: ";
cin >> newContact.phone;
cout << "请输入邮箱地址: ";
cin >> newContact.email;
contacts.push_back(newContact);
cout << "联系人添加成功!" << endl;
}
// 显示所有联系人
void displayContacts(const vector& contacts) {
if (contacts.empty()) {
cout << "通讯录为空!" << endl;
} else {
cout << "通讯录中的联系人如下:" << endl;
for (const auto& contact : contacts) {
cout << "姓名: " << contact.name << " 电话: " << contact.phone << " 邮箱: " << contact.email << endl;
}
}
}
// 主菜单
void mainMenu(vector& contacts) {
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加联系人" << endl;
cout << "2. 显示所有联系人" << endl;
cout << "3. 退出程序" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addContact(contacts);
break;
case 2:
displayContacts(contacts);
break;
case 3:
cout << "感谢使用通讯录管理系统,再见!" << endl;
return;
default:
cout << "输入无效,请重新选择操作!" << endl;
break;
}
}
}
int main() {
vector contacts;
mainMenu(contacts);
return 0;
}
用户评论