鱼C论坛

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

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

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

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

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

x
  1. #include<iostream>
  2. using namespace std;
  3. #include<string>
  4. #define MAX 1000

  5. struct person
  6. {
  7.         string name;
  8.         int sex;//1男   2女
  9.         int age;
  10.         string phone;
  11.         string add;
  12. };

  13. struct addbook
  14. {
  15.         struct person personArray[MAX];
  16.         int size;
  17. };

  18. void showMenu()
  19. {
  20.         cout << "*************************" << endl;
  21.         cout << "***** 1、添加联系人 *****" << endl;
  22.         cout << "***** 2、显示联系人 *****" << endl;
  23.         cout << "***** 3、删除联系人 *****" << endl;
  24.         cout << "***** 4、查找联系人 *****" << endl;
  25.         cout << "***** 5、修改联系人 *****" << endl;
  26.         cout << "***** 6、清空通讯录 *****" << endl;
  27.         cout << "***** 0、退出通讯录 *****" << endl;
  28.         cout << "*************************" << endl;

  29. }

  30. void addPerson(struct addbook *book)
  31. {
  32.         if (book->size == MAX)
  33.         {
  34.                 cout << "联系人已满" << endl;
  35.                 return;

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

  43.                 int sex;
  44.                 cout << "请输入联系人性别    1、男    2、女" << endl;
  45.                 while (true)
  46.                 {
  47.                         cin >> sex;
  48.                         if (sex == 1 || sex == 2)
  49.                         {
  50.                                 book->personArray[book->size].sex = sex;
  51.                                 break;
  52.                         }
  53.                         cout << "输入有误请重新输入" << endl;
  54.                 }                               

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

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

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

  67.                 book->size++;
  68.                 cout << "添加成功" << endl;
  69.                 system("pause");
  70.                 system("cls");
  71.         }

  72. }

  73. void showPerson(struct addbook book)
  74. {
  75.         if (book.size == 0)
  76.         {
  77.                 cout << " 当前记录为空" << endl;
  78.         }
  79.         for (int i = 0; i < book.size; i++)
  80.         {
  81.                 cout << "姓名:" << book.personArray[i].name << "\t";
  82.                 cout << "性别:" << (book.personArray[i].sex == 1 ? "男":"女") << "\t";
  83.                 cout << "年龄:" << book.personArray[i].age << "\t";
  84.                 cout << "电话:" << book.personArray[i].phone << "\t";
  85.                 cout << "住址:" << book.personArray[i].add << endl;
  86.         };
  87.         /*system("pause");
  88.         system("cls");*/
  89. }

  90. int isExist(struct addbook *book,string name)
  91. {
  92.         for (int i = 0; i < book->size; i++)
  93.         {
  94.                 if (book->personArray[i].name == name)
  95.                 {
  96.                         return i;
  97.                 }
  98.                 return -1;
  99.         }
  100. }

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

  104.         string name;
  105.         cin >> name;

  106.         int ret = isExist(book, name);
  107.         if (ret != -1)
  108.         {
  109.                 for (int i = ret; i < book->size; i++)
  110.                 {
  111.                         book->personArray[ret] = book->personArray[ret + 1];
  112.                 }
  113.                 book->size--;
  114.                 cout << "删除成功" << endl;
  115.         }
  116.         else
  117.         {
  118.                 cout << "查无此人" << endl;
  119.         }
  120. }

  121. void findPerson(struct addbook *book)
  122. {
  123.         cout << "请输入查找联系人姓名" << endl;
  124.         string name;
  125.         cin >> name;
  126.         int ret = isExist(book, name);
  127.         if (ret != -1)
  128.         {
  129.                 cout << "姓名:" << book->personArray[ret].name << "\t";
  130.                 cout << "性别:" << (book->personArray[ret].sex == 1 ? "男" : "女") << "\t";
  131.                 cout << "年龄:" << book->personArray[ret].age << "\t";
  132.                 cout << "电话:" << book->personArray[ret].phone << "\t";
  133.                 cout << "住址:" << book->personArray[ret].add << endl;
  134.         }
  135.         else
  136.         {
  137.                 cout << "查无此人" << endl;
  138.         }
  139.        
  140. }
  141. int main()
  142. {
  143.         struct addbook book;
  144.         book.size = 0;
  145.         int select = 0;
  146.         while (1)
  147.         {
  148.                 showMenu();
  149.                
  150.                 cin >> select;
  151.                 switch (select)
  152.                 {
  153.                 case 1:addPerson(&book);//利用地址传递修改
  154.                         break;
  155.                 case 2: showPerson(book);
  156.                         break;

  157.                 case 3:deletePerson(&book);
  158.                         break;

  159.                 case 4:findPerson(&book);
  160.                         break;

  161.                 case 5:
  162.                         break;

  163.                 case 6:
  164.                         break;

  165.                 case 0:
  166.                         cout << "欢迎下次使用" << endl;
  167.                         system("pause");
  168.                         return 0;
  169.                         break;
  170.                 default:
  171.                         break;
  172.                 }

  173.         }
  174.        

  175.         system("pause");
  176.         return 0;
  177. }
复制代码

22.png
24.png
每次都是只能找到张三,查找后面的人就变成查无此人
最佳答案
2022-2-8 13:30:03
  1. int isExist(struct addbook *book, string name)
  2. {
  3.     for (int i = 0; i < book->size; i++)
  4.     {
  5.         if (book->personArray[i].name == name)
  6.         {
  7.             return i;
  8.         }
  9.     }
  10.     return -1;  // 应该在 for 循环外面
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-2-8 13:30:03 | 显示全部楼层    本楼为最佳答案   
  1. int isExist(struct addbook *book, string name)
  2. {
  3.     for (int i = 0; i < book->size; i++)
  4.     {
  5.         if (book->personArray[i].name == name)
  6.         {
  7.             return i;
  8.         }
  9.     }
  10.     return -1;  // 应该在 for 循环外面
  11. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

  3. using namespace std;

  4. #define MAX 1000

  5. struct person {
  6.     string name;
  7.     int sex; // 1男   2女
  8.     int age;
  9.     string phone;
  10.     string add;
  11. };

  12. struct addbook {
  13.     struct person personArray[MAX];
  14.     int size;
  15. };

  16. void showMenu() {
  17.     cout << "*************************" << endl;
  18.     cout << "***** 1、添加联系人 *****" << endl;
  19.     cout << "***** 2、显示联系人 *****" << endl;
  20.     cout << "***** 3、删除联系人 *****" << endl;
  21.     cout << "***** 4、查找联系人 *****" << endl;
  22.     cout << "***** 5、修改联系人 *****" << endl;
  23.     cout << "***** 6、清空通讯录 *****" << endl;
  24.     cout << "***** 0、退出通讯录 *****" << endl;
  25.     cout << "*************************" << endl;
  26. }

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

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

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

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

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

  58.     book->size++;
  59.     cout << "添加成功" << endl;
  60.     //system("pause");
  61.     //system("cls");
  62. }

  63. void showPerson(struct addbook book) {
  64.     if(book.size == 0) {
  65.         cout << " 当前记录为空" << endl;
  66.     }
  67.     for(int i = 0; i < book.size; i++) {
  68.         cout << "姓名:" << book.personArray[i].name << "\t";
  69.         cout << "性别:" << (book.personArray[i].sex == 1 ? "男" : "女")
  70.              << "\t";
  71.         cout << "年龄:" << book.personArray[i].age << "\t";
  72.         cout << "电话:" << book.personArray[i].phone << "\t";
  73.         cout << "住址:" << book.personArray[i].add << endl;
  74.     };
  75.     /*system("pause");
  76.     system("cls");*/
  77. }

  78. int isExist(struct addbook *book, string name) {
  79.     for(int i = 0; i < book->size; i++) {
  80.         if(book->personArray[i].name == name) {
  81.             return i;
  82.         }
  83.         //return -1;      // ???
  84.     }
  85.     return -1;
  86. }

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

  89.     string name;
  90.     cin >> name;

  91.     int ret = isExist(book, name);
  92.     if(ret != -1) {
  93.         //for(int i = ret; i < book->size; i++) {
  94.         for(int i = ret; i < book->size - 1; i++) {
  95.             book->personArray[ret] = book->personArray[ret + 1];
  96.         }
  97.         book->size--;
  98.         cout << "删除成功" << endl;
  99.     } else {
  100.         cout << "查无此人" << endl;
  101.     }
  102. }

  103. void findPerson(struct addbook *book) {
  104.     cout << "请输入查找联系人姓名" << endl;
  105.     string name;
  106.     cin >> name;
  107.     int ret = isExist(book, name);
  108.     if(ret != -1) {
  109.         cout << "姓名:" << book->personArray[ret].name << "\t";
  110.         cout << "性别:" << (book->personArray[ret].sex == 1 ? "男" : "女")
  111.              << "\t";
  112.         cout << "年龄:" << book->personArray[ret].age << "\t";
  113.         cout << "电话:" << book->personArray[ret].phone << "\t";
  114.         cout << "住址:" << book->personArray[ret].add << endl;
  115.     } else {
  116.         cout << "查无此人" << endl;
  117.     }
  118. }

  119. int main() {
  120.     struct addbook book;
  121.     book.size = 0;
  122.     int select = 0;
  123.     while(1) {
  124.         showMenu();

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

  134.         case 3:
  135.             deletePerson(&book);
  136.             break;

  137.         case 4:
  138.             findPerson(&book);
  139.             break;

  140.         case 5:
  141.             break;

  142.         case 6:
  143.             break;

  144.         case 0:
  145.             cout << "欢迎下次使用" << endl;
  146.             //system("pause");
  147.             return 0;
  148.             break;
  149.         default:
  150.             break;
  151.         }
  152.     }

  153.     //system("pause");
  154.     return 0;
  155. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

  6. #define MAX 1000

  7. struct person {
  8.     string name;
  9.     int sex; // 1男   2女
  10.     int age;
  11.     string phone;
  12.     string add;
  13. };

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

  18. void showMenu() {
  19.     cout << "*************************" << endl;
  20.     cout << "***** 1、添加联系人 *****" << endl;
  21.     cout << "***** 2、显示联系人 *****" << endl;
  22.     cout << "***** 3、删除联系人 *****" << endl;
  23.     cout << "***** 4、查找联系人 *****" << endl;
  24.     cout << "***** 5、修改联系人 *****" << endl;
  25.     cout << "***** 6、清空通讯录 *****" << endl;
  26.     cout << "***** 0、退出通讯录 *****" << endl;
  27.     cout << "*************************" << endl;
  28. }

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

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

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

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

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

  60.     book->size++;
  61.     cout << "添加成功" << endl;
  62.     //system("pause");
  63.     //system("cls");
  64. }

  65. //void showPerson(struct addbook book) {
  66. void showPerson(const struct addbook *book) {
  67.     if(book->size == 0) {
  68.         cout << " 当前记录为空" << endl;
  69.     }
  70.     for(int i = 0; i < book->size; i++) {
  71.         cout << "姓名:" << book->personArray[i].name << "\t";
  72.         cout << "性别:" << (book->personArray[i].sex == 1 ? "男" : "女")
  73.              << "\t";
  74.         cout << "年龄:" << book->personArray[i].age << "\t";
  75.         cout << "电话:" << book->personArray[i].phone << "\t";
  76.         cout << "住址:" << book->personArray[i].add << endl;
  77.     };
  78.     /*system("pause");
  79.     system("cls");*/
  80. }

  81. //int isExist(struct addbook *book, string name) {
  82. int isExist(const struct addbook *book, string name) {
  83.     for(int i = 0; i < book->size; i++) {
  84.         if(book->personArray[i].name == name) {
  85.             return i;
  86.         }
  87.         //return -1;      // ???
  88.     }
  89.     return -1;
  90. }

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

  93.     string name;
  94.     cin >> name;

  95.     int ret = isExist(book, name);
  96.     if(ret != -1) {
  97.         //for(int i = ret; i < book->size; i++) {
  98.         for(int i = ret; i < book->size - 1; i++) {
  99.             book->personArray[ret] = book->personArray[ret + 1];
  100.         }
  101.         book->size--;
  102.         cout << "删除成功" << endl;
  103.     } else {
  104.         cout << "查无此人" << endl;
  105.     }
  106. }

  107. //void findPerson(struct addbook *book) {
  108. void findPerson(const struct addbook *book) {
  109.     cout << "请输入查找联系人姓名" << endl;
  110.     string name;
  111.     cin >> name;
  112.     int ret = isExist(book, name);
  113.     if(ret != -1) {
  114.         cout << "姓名:" << book->personArray[ret].name << "\t";
  115.         cout << "性别:" << (book->personArray[ret].sex == 1 ? "男" : "女")
  116.              << "\t";
  117.         cout << "年龄:" << book->personArray[ret].age << "\t";
  118.         cout << "电话:" << book->personArray[ret].phone << "\t";
  119.         cout << "住址:" << book->personArray[ret].add << endl;
  120.     } else {
  121.         cout << "查无此人" << endl;
  122.     }
  123. }

  124. int main() {
  125.     struct addbook book;
  126.     book.size = 0;
  127.     int select = 0;
  128.     while(1) {
  129.         showMenu();

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

  140.         case 3:
  141.             deletePerson(&book);
  142.             break;

  143.         case 4:
  144.             findPerson(&book);
  145.             break;

  146.         case 5:
  147.             break;

  148.         case 6:
  149.             break;

  150.         case 0:
  151.             cout << "欢迎下次使用" << endl;
  152.             //system("pause");
  153.             return 0;
  154.             break;
  155.         default:
  156.             break;
  157.         }
  158.     }

  159.     //system("pause");
  160.     return 0;
  161. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-8 13:49:46 | 显示全部楼层
小甲鱼最新课程 -> https://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
  1. using namespace std;    // 为什么要禁用C++的名字空间?
  2.                         // C++为什么要引入名字空间?
  3.                         // 是为了解决什么问题?
复制代码


其实这个我也不知道是什么意思,只是每次学习视频上都会有这个,老师说是固定的结构框架
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

使用道具 举报

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

  2. using std::cout, std::endl;

  3. int main() {
  4.     cout << "hello world!" << endl;
  5.     return 0;
  6. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

谢谢,以后我也按照这样写
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

22.png

但是我用这个开头会报错呢
小甲鱼最新课程 -> https://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
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

不是很懂这个,不知道怎么弄
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-9 13:05:41 | 显示全部楼层
小甲鱼最新课程 -> https://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

成功了!!!谢谢大佬!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-24 22:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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