c++实例之通讯录管理系统之添加联系人功能(二)
添加联系人
1.设计联系人结构体
2.设计通讯录结构体
3.main函数中创建通讯录
4.分装添加联系人函数
5.测试添加联系人功能
#include<iostream> using namespace std; constexpr auto MAX = 1000; //联系人结构体 struct Person { string m_name; int m_sex; int m_age; string m_phone; string m_address; }; //通讯录结构体 struct AddressBooks { //联系人数组 struct Person personArray[MAX]; //记录联系人个数 int m_size; }; //添加人员 void addPerson(AddressBooks* abs) { //判断通讯录是否已满,已满就不再添加 if (abs->m_size == MAX) { cout << "通讯录已满" << endl; } else{ string name; int sex; int age; string phone; string address; cout << "请输入姓名:"; cin >> name; while (true) { cout << "请输入性别(0代表男,1代表女):"; cin >> sex; if (sex == 0 || sex == 1) { break; }else{ cout << "您输入有误,请重新输入!" << endl; } } cout << "请输入年龄:"; cin >> age; cout << "请输入电话:"; cin >> phone; cout << "请输入地址:"; cin >> address; abs->personArray[abs->m_size].m_name = name; abs->personArray[abs->m_size].m_sex = sex; abs->personArray[abs->m_size].m_age = age; abs->personArray[abs->m_size].m_phone = phone; abs->personArray[abs->m_size].m_address = address; abs->m_size++; cout << "添加成功!" << endl; //请按任意键继续 system("pause"); //清屏 system("cls"); } } //菜单界面 void showMenu() { cout << "************************" << endl; cout << "***** 1.添加联系人 *****" << endl; cout << "***** 2.显示联系人 *****" << endl; cout << "***** 3.删除联系人 *****" << endl; cout << "***** 4.修改联系人 *****" << endl; cout << "***** 5.查找联系人 *****" << endl; cout << "***** 6.清空联系人 *****" << endl; cout << "***** 0.退出通讯录 *****" << endl; cout << "************************" << endl; } int main() { //创建通讯录结构体变量 AddressBooks abs; abs.m_size = 0; int select = 0; while(true){ showMenu(); cout << "请输入相应的选项:" << endl; cin >> select; switch (select){ case 1://添加 addPerson(&abs); break; case 2://显示 break; case 3://删除 break; case 4://修改 break; case 5://查找 break; case 6://清空 break; case 0://退出 cout << "欢迎下次使用" << endl; system("pause"); return 0; break; } } }