鱼C论坛

 找回密码
 立即注册
查看: 965|回复: 13

[已解决]为什么这个函数只能找到第一个联系人

[复制链接]
发表于 2022-2-8 13:23:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
using namespace std;
#include<string>
#define MAX 1000

struct person
{
        string name;
        int sex;//1男   2女
        int age;
        string phone;
        string add;
};

struct addbook
{
        struct person personArray[MAX];
        int size;
};

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;

}

void addPerson(struct addbook *book)
{
        if (book->size == MAX)
        {
                cout << "联系人已满" << endl;
                return;

        }
        else
        {
                string name;
                cout << "请输入联系人姓名" << endl;
                cin >> name;
                book->personArray[book->size].name = name;

                int sex;
                cout << "请输入联系人性别    1、男    2、女" << endl;
                while (true)
                {
                        cin >> sex;
                        if (sex == 1 || sex == 2)
                        {
                                book->personArray[book->size].sex = sex;
                                break;
                        }
                        cout << "输入有误请重新输入" << endl;
                }                                

                int age;
                cout << "请输入联系人年龄" << endl;
                cin >> age;
                book->personArray[book->size].age = age;

                string phone;
                cout << "请输入联系人电话" << endl;
                cin >> phone;
                book->personArray[book->size].phone = phone;

                string add;
                cout << "请输入联系人住址" << endl;
                cin >> add;
                book->personArray[book->size].add = add;

                book->size++;
                cout << "添加成功" << endl;
                system("pause");
                system("cls");
        }

}

void showPerson(struct addbook book)
{
        if (book.size == 0)
        {
                cout << " 当前记录为空" << endl;
        }
        for (int i = 0; i < book.size; i++)
        {
                cout << "姓名:" << book.personArray[i].name << "\t";
                cout << "性别:" << (book.personArray[i].sex == 1 ? "男":"女") << "\t";
                cout << "年龄:" << book.personArray[i].age << "\t";
                cout << "电话:" << book.personArray[i].phone << "\t";
                cout << "住址:" << book.personArray[i].add << endl;
        };
        /*system("pause");
        system("cls");*/
}

int isExist(struct addbook *book,string name)
{
        for (int i = 0; i < book->size; i++)
        {
                if (book->personArray[i].name == name)
                {
                        return i;
                }
                return -1;
        }
}

void deletePerson(struct addbook *book)
{
        cout << "请输入需要删除的联系人" << endl;

        string name;
        cin >> name;

        int ret = isExist(book, name);
        if (ret != -1)
        {
                for (int i = ret; i < book->size; i++)
                {
                        book->personArray[ret] = book->personArray[ret + 1];
                }
                book->size--;
                cout << "删除成功" << endl;
        }
        else
        {
                cout << "查无此人" << endl;
        }
}

void findPerson(struct addbook *book)
{
        cout << "请输入查找联系人姓名" << endl;
        string name;
        cin >> name;
        int ret = isExist(book, name);
        if (ret != -1)
        {
                cout << "姓名:" << book->personArray[ret].name << "\t";
                cout << "性别:" << (book->personArray[ret].sex == 1 ? "男" : "女") << "\t";
                cout << "年龄:" << book->personArray[ret].age << "\t";
                cout << "电话:" << book->personArray[ret].phone << "\t";
                cout << "住址:" << book->personArray[ret].add << endl;
        }
        else
        {
                cout << "查无此人" << endl;
        }
        
}
int main()
{
        struct addbook book;
        book.size = 0;
        int select = 0;
        while (1)
        {
                showMenu();
                
                cin >> select;
                switch (select)
                {
                case 1:addPerson(&book);//利用地址传递修改
                        break;
                case 2: showPerson(book);
                        break;

                case 3:deletePerson(&book);
                        break;

                case 4:findPerson(&book);
                        break;

                case 5:
                        break;

                case 6:
                        break;

                case 0:
                        cout << "欢迎下次使用" << endl;
                        system("pause");
                        return 0;
                        break;
                default:
                        break;
                }

        } 
        

        system("pause");
        return 0;
}
22.png
24.png
每次都是只能找到张三,查找后面的人就变成查无此人
最佳答案
2022-2-8 13:30:03
int isExist(struct addbook *book, string name)
{
    for (int i = 0; i < book->size; i++)
    {
        if (book->personArray[i].name == name)
        {
            return i;
        }
    }
    return -1;  // 应该在 for 循环外面
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-2-8 13:30:03 | 显示全部楼层    本楼为最佳答案   
int isExist(struct addbook *book, string name)
{
    for (int i = 0; i < book->size; i++)
    {
        if (book->personArray[i].name == name)
        {
            return i;
        }
    }
    return -1;  // 应该在 for 循环外面
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-8 13:41:05 | 显示全部楼层
#include <iostream>
#include <string>

using namespace std;

#define MAX 1000

struct person {
    string name;
    int sex; // 1男   2女
    int age;
    string phone;
    string add;
};

struct addbook {
    struct person personArray[MAX];
    int size;
};

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;
}

void addPerson(struct addbook *book) {
    if(book->size == MAX) {
        cout << "联系人已满" << endl;
        return;
    }
    string name;
    cout << "请输入联系人姓名" << endl;
    cin >> name;
    book->personArray[book->size].name = name;

    int sex;
    cout << "请输入联系人性别    1、男    2、女" << endl;
    while(true) {
        cin >> sex;
        if(sex == 1 || sex == 2) {
            book->personArray[book->size].sex = sex;
            break;
        }
        cout << "输入有误请重新输入" << endl;
    }

    int age;
    cout << "请输入联系人年龄" << endl;
    cin >> age;
    book->personArray[book->size].age = age;

    string phone;
    cout << "请输入联系人电话" << endl;
    cin >> phone;
    book->personArray[book->size].phone = phone;

    string add;
    cout << "请输入联系人住址" << endl;
    cin >> add;
    book->personArray[book->size].add = add;

    book->size++;
    cout << "添加成功" << endl;
    //system("pause");
    //system("cls");
}

void showPerson(struct addbook book) {
    if(book.size == 0) {
        cout << " 当前记录为空" << endl;
    }
    for(int i = 0; i < book.size; i++) {
        cout << "姓名:" << book.personArray[i].name << "\t";
        cout << "性别:" << (book.personArray[i].sex == 1 ? "男" : "女")
             << "\t";
        cout << "年龄:" << book.personArray[i].age << "\t";
        cout << "电话:" << book.personArray[i].phone << "\t";
        cout << "住址:" << book.personArray[i].add << endl;
    };
    /*system("pause");
    system("cls");*/
}

int isExist(struct addbook *book, string name) {
    for(int i = 0; i < book->size; i++) {
        if(book->personArray[i].name == name) {
            return i;
        }
        //return -1;      // ???
    }
    return -1;
}

void deletePerson(struct addbook *book) {
    cout << "请输入需要删除的联系人" << endl;

    string name;
    cin >> name;

    int ret = isExist(book, name);
    if(ret != -1) {
        //for(int i = ret; i < book->size; i++) {
        for(int i = ret; i < book->size - 1; i++) {
            book->personArray[ret] = book->personArray[ret + 1];
        }
        book->size--;
        cout << "删除成功" << endl;
    } else {
        cout << "查无此人" << endl;
    }
}

void findPerson(struct addbook *book) {
    cout << "请输入查找联系人姓名" << endl;
    string name;
    cin >> name;
    int ret = isExist(book, name);
    if(ret != -1) {
        cout << "姓名:" << book->personArray[ret].name << "\t";
        cout << "性别:" << (book->personArray[ret].sex == 1 ? "男" : "女")
             << "\t";
        cout << "年龄:" << book->personArray[ret].age << "\t";
        cout << "电话:" << book->personArray[ret].phone << "\t";
        cout << "住址:" << book->personArray[ret].add << endl;
    } else {
        cout << "查无此人" << endl;
    }
}

int main() {
    struct addbook book;
    book.size = 0;
    int select = 0;
    while(1) {
        showMenu();

        cin >> select;
        switch(select) {
        case 1:
            addPerson(&book); //利用地址传递修改
            break;
        case 2:
            showPerson(book);   // 知道这么写意味着什么吗?这会把book整个复制一份
                                // 把复制的这个book传递给showPerson
            break;

        case 3:
            deletePerson(&book);
            break;

        case 4:
            findPerson(&book);
            break;

        case 5:
            break;

        case 6:
            break;

        case 0:
            cout << "欢迎下次使用" << endl;
            //system("pause");
            return 0;
            break;
        default:
            break;
        }
    }

    //system("pause");
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-8 13:47:08 | 显示全部楼层
#include <iostream>
#include <string>

using namespace std;    // 为什么要禁用C++的名字空间?
                        // C++为什么要引入名字空间?
                        // 是为了解决什么问题?

#define MAX 1000

struct person {
    string name;
    int sex; // 1男   2女
    int age;
    string phone;
    string add;
};

struct addbook {
    struct person personArray[MAX];     // 为什么不用vector ?
    int size;
};

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;
}

void addPerson(struct addbook *book) {
    if(book->size == MAX) {
        cout << "联系人已满" << endl;
        return;
    }
    string name;
    cout << "请输入联系人姓名" << endl;
    cin >> name;
    book->personArray[book->size].name = name;

    int sex;
    cout << "请输入联系人性别    1、男    2、女" << endl;
    while(true) {
        cin >> sex;
        if(sex == 1 || sex == 2) {
            book->personArray[book->size].sex = sex;
            break;
        }
        cout << "输入有误请重新输入" << endl;
    }

    int age;
    cout << "请输入联系人年龄" << endl;
    cin >> age;
    book->personArray[book->size].age = age;

    string phone;
    cout << "请输入联系人电话" << endl;
    cin >> phone;
    book->personArray[book->size].phone = phone;

    string add;
    cout << "请输入联系人住址" << endl;
    cin >> add;
    book->personArray[book->size].add = add;

    book->size++;
    cout << "添加成功" << endl;
    //system("pause");
    //system("cls");
}

//void showPerson(struct addbook book) {
void showPerson(const struct addbook *book) {
    if(book->size == 0) {
        cout << " 当前记录为空" << endl;
    }
    for(int i = 0; i < book->size; i++) {
        cout << "姓名:" << book->personArray[i].name << "\t";
        cout << "性别:" << (book->personArray[i].sex == 1 ? "男" : "女")
             << "\t";
        cout << "年龄:" << book->personArray[i].age << "\t";
        cout << "电话:" << book->personArray[i].phone << "\t";
        cout << "住址:" << book->personArray[i].add << endl;
    };
    /*system("pause");
    system("cls");*/
}

//int isExist(struct addbook *book, string name) {
int isExist(const struct addbook *book, string name) {
    for(int i = 0; i < book->size; i++) {
        if(book->personArray[i].name == name) {
            return i;
        }
        //return -1;      // ???
    }
    return -1;
}

void deletePerson(struct addbook *book) {
    cout << "请输入需要删除的联系人" << endl;

    string name;
    cin >> name;

    int ret = isExist(book, name);
    if(ret != -1) {
        //for(int i = ret; i < book->size; i++) {
        for(int i = ret; i < book->size - 1; i++) {
            book->personArray[ret] = book->personArray[ret + 1];
        }
        book->size--;
        cout << "删除成功" << endl;
    } else {
        cout << "查无此人" << endl;
    }
}

//void findPerson(struct addbook *book) {
void findPerson(const struct addbook *book) {
    cout << "请输入查找联系人姓名" << endl;
    string name;
    cin >> name;
    int ret = isExist(book, name);
    if(ret != -1) {
        cout << "姓名:" << book->personArray[ret].name << "\t";
        cout << "性别:" << (book->personArray[ret].sex == 1 ? "男" : "女")
             << "\t";
        cout << "年龄:" << book->personArray[ret].age << "\t";
        cout << "电话:" << book->personArray[ret].phone << "\t";
        cout << "住址:" << book->personArray[ret].add << endl;
    } else {
        cout << "查无此人" << endl;
    }
}

int main() {
    struct addbook book;
    book.size = 0;
    int select = 0;
    while(1) {
        showMenu();

        cin >> select;
        switch(select) {
        case 1:
            addPerson(&book); //利用地址传递修改
            break;
        case 2:
            //showPerson(book);   // 知道这么写意味着什么吗?这会把book整个复制一份
            //                    // 把复制的这个book传递给showPerson
            showPerson(&book);
            break;

        case 3:
            deletePerson(&book);
            break;

        case 4:
            findPerson(&book);
            break;

        case 5:
            break;

        case 6:
            break;

        case 0:
            cout << "欢迎下次使用" << endl;
            //system("pause");
            return 0;
            break;
        default:
            break;
        }
    }

    //system("pause");
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-8 13:49:46 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-8 14:23:59 | 显示全部楼层
人造人 发表于 2022-2-8 13:49
https://fishc.com.cn/forum.php?mod=redirect&goto=findpost&ptid=208317&pid=5714343
using namespace std;    // 为什么要禁用C++的名字空间?
                        // C++为什么要引入名字空间?
                        // 是为了解决什么问题?

其实这个我也不知道是什么意思,只是每次学习视频上都会有这个,老师说是固定的结构框架
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-8 14:30:24 | 显示全部楼层
孤世星辰 发表于 2022-2-8 14:23
其实这个我也不知道是什么意思,只是每次学习视频上都会有这个,老师说是固定的结构框架

这么写有时候会出问题,而且有时候是很不好解决,因为你根本就察觉不到是因为名字冲突导致的
这么写确实可以偷懒,但是换来的是几个小时的debug,我认为不值得,我反正是不这么写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-8 14:32:04 | 显示全部楼层
推荐这样
#include <iostream>

using std::cout, std::endl;

int main() {
    cout << "hello world!" << endl;
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-8 19:49:47 | 显示全部楼层

谢谢,以后我也按照这样写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-8 19:53:39 | 显示全部楼层

22.png

但是我用这个开头会报错呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-8 20:06:43 | 显示全部楼层
孤世星辰 发表于 2022-2-8 19:53
但是我用这个开头会报错呢

这是C++17的内容
在设置里面找一找吧,vs应该支持C++17,大概
https://blog.csdn.net/tcy23456/article/details/105080261
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-9 13:04:52 | 显示全部楼层
人造人 发表于 2022-2-8 20:06
这是C++17的内容
在设置里面找一找吧,vs应该支持C++17,大概
https://blog.csdn.net/tcy23456/article ...

不是很懂这个,不知道怎么弄
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-9 13:05:41 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-9 19:29:06 | 显示全部楼层
人造人 发表于 2022-2-9 13:05
https://www.baidu.com/baidu?tn=monline_7_dg&ie=utf-8&wd=vs+c%2B%2B17

成功了!!!谢谢大佬!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-30 19:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表