m0_74071629 2023-03-25 12:41 采纳率: 100%
浏览 68
已结题

线性表实现通讯录管理系统(语言-c++)

通讯录管理系统
添加联系人,输入信息后无法显示后续内容
报错:

img


求解答,感谢

//题目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;
}

  • 写回答

1条回答 默认 最新

  • threenewbee 2023-03-25 12:57
    关注

    你的程序一上来静态分配1000个结构体,编译器提醒你这样堆栈浪费太大。尽量用动态分配吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月2日
  • 已采纳回答 3月25日
  • 创建了问题 3月25日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效