通讯录管理系统
添加联系人,输入信息后无法显示后续内容
报错:
求解答,感谢
//题目3——应用实验
#include<iostream>
using namespace std;
#include<string>
#define MAX 1000
#include<iomanip>
void menu()//展示菜单栏
{
cout << "--------------------------------------" << endl;
cout << "------------通讯录管理系统------------" << endl;
cout << "------------1.添加联系人--------------" << endl;
cout << "------------2.删除联系人--------------" << endl;
cout << "------------3.查找联系人--------------" << endl;
cout << "------------4.更改联系人信息----------" << endl;
cout << "------------5.显示通讯录--------------" << endl;
cout << "------------0.退出通讯录--------------" << endl;
cout << "--------------------------------------" << endl;
}
struct DataType//通讯录要存储的信息
{
int ID;//编号
string name;//姓名
string ch;//性别
string phone;//电话
string addr;//地址
};
struct Data_s
{
struct DataType people[MAX];//通讯录能保存的最大人数
int length = 0;//通讯录中已有人数
};
void Assign(Data_s* Ds)
{
int ID_;//ID
cout << "请输入联系人ID:";
cin >> ID_;
Ds->people[Ds->length].ID = ID_;
string name_;//姓名
cout << "请输入联系人姓名:";
cin >> name_;
Ds->people[Ds->length].name = name_;
string ch_;//性别
cout << "请输入联系人性别:";
cin >> ch_;
Ds->people[Ds->length].ch = ch_;
string phone_;//电话
cout << "请输入联系人电话号码:";
cin >> phone_;
Ds->people[Ds->length].phone = phone_;
string addr_;//地址
cout << "请输入联系人地址:";
cin >> addr_;
Ds->people[Ds->length].addr = addr_;
}
void Assign(Data_s* Ds, int num)
{
int ID_;//ID
cout << "请输入联系人ID:";
cin >> ID_;
Ds->people[num].ID = ID_;
string name_;//姓名
cout << "请输入联系人姓名:";
cin >> name_;
Ds->people[num].name = name_;
string ch_;//性别
cout << "请输入联系人性别:";
cin >> ch_;
Ds->people[num].ch = ch_;
string phone_;//电话
cout << "请输入联系人电话号码:";
cin >> phone_;
Ds->people[num].phone = phone_;
string addr_;//地址
cout << "请输入联系人地址:";
cin >> addr_;
Ds->people[num].addr = addr_;
}
void ADD(Data_s* Ds)
{
//首先判断通讯录是否已满
if (Ds->length == MAX)
cout << "通讯录已满";
cout << "请根据提示输入联系人相关信息" << endl;
Assign(Ds);
Ds->length++;
cout << "新联系人已添加成功!" << endl;
system("cls");//清屏
}
int existance(Data_s* Ds, string name)//根据姓名查找联系人是否存在,若存在,返回该联系人所属下标
{
for (int i = 0; i < Ds->length; i++)
{
if (Ds->people->name == name)
return i;
}
return -1;//查找失败
}
void ShowLine()
{
int j;
for (j = 0; j < 36; j++)
{
cout << "——";
}
cout << endl;
}
void DELETE(Data_s* Ds, int num)
{
DataType ds = Ds->people[num];
for (int i = 0; i < Ds->length - 1; i++)
Ds->people[i] = Ds->people[i + 1];
Ds->length--;
}
void ShowHeading()
{
//打印标题
ShowLine();
cout << "|\t\t\t\t通讯录\t\t\t\t\t|" << endl;
ShowLine();
cout << "|ID\t\t|姓名\t|性别\t|电话\t\t|地址\t\t\t|\n";
ShowLine();
}
void SHOW(Data_s* Ds)
{
if (Ds->length == 0)
{
cout << "通讯录为空,请添加联系人!" << endl;
system("pause");
system("cls");
return;
}
//打印标题
ShowHeading();
//输出内容
for (int i = 0; i < Ds->length; i++)
{
cout << "|" << Ds->people[i].ID << "\t" << "|" << Ds->people[i].name << "\t"
<< "|" << Ds->people[i].ch << "\t" << "|" << Ds->people[i].phone << "\t"
<< "|" << Ds->people[i].addr << endl;
ShowLine();
}
//清屏
system("pause");
system("cls");
}
int main()
{
Data_s Ds;//创建通讯录
Ds.length = 0;
while (true)
{
int select = 0;//用户选择输入的数字
menu();//展示菜单栏
cin >> select;
switch (select)
{
case 1://添加联系人ADD
ADD(&Ds);
break;
case 2://删除联系人DELETE
{
cout << "请输入要删除的联系人姓名";
string name;
cin >> name;
int num;
num = existance(&Ds, name);
if (num == -1)
{
cout << "您要删除的联系人不存在" << endl;
}
else
{
DELETE(&Ds, num);
cout << name << "已被移除通讯录" << endl;
}
}
break;
case 3://查找联系人REARCH
{
cout << "请输入要查找的联系人姓名";
string name;
cin >> name;
int num;
num = existance(&Ds, name);
if (num == -1)
{
cout << "您所查找的联系人不存在" << endl;
}
else
{
ShowHeading();
cout << "|" << Ds.people[num].ID << "\t" << "|" << Ds.people[num].name << "\t"
<< "|" << Ds.people[num].ch << "\t" << "|" << Ds.people[num].phone << "\t"
<< "|" << Ds.people[num].addr << endl;
ShowLine();
}
system("pause");
system("cls");
}
break;
case 4://更改通讯录ALTER
{
cout << "请输入要更改信息的联系人姓名:";
string name;
cin >> name;
int num;
num = existance(&Ds, name);
if (num == -1)
{
cout << "您所查找的联系人不存在" << endl;
}
else
{
Assign(&Ds, num);
cout << "已更改成功!" << endl;
system("pause");
system("cls");
}
}
break;
case 5://显示通讯录SHOW
SHOW(&Ds);
break;
case 0://退出通讯录EXIT
cout << "您已退出通讯录" << endl;
system("pause");
return 0;
break;
}
}
return 0;
}