免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2098 | 回复: 3

[C] [讨论]是strcmp的问题还是其他地方出错? [复制链接]

论坛徽章:
0
发表于 2009-01-17 01:00 |显示全部楼层
5可用积分
下面的代码是让用户输入一组人名汉语拼音的首字母组合并存入一个二维字符串数组,然后让他再单独输入一个人名汉语拼音首字母组合(一维字符串),看看这个字母组合在不在刚刚输入的那组二维字符串数组中,如果在就输出该拼音在二维字符串数组中的位置。结果出现了找不到输入的正确的拼音字符串的问题。

/*Find the pinyin initials*/

#include <stdio.h>
#include <string.h>

#define STUNUM 60
#define NMLEN 3

int main(void){
   
    char papernames[STUNUM][NMLEN], inputname[NMLEN];
    int papernum = 0;
   
    printf("\nInput students' names in pinyin ( Input Enter at the beginning of "
           "a line to quit):\n");
    while(papernum < 60 && gets(papernames[papernum]) != NULL &&
     papernames[papernum][0] != '\0')
           papernum++;
   
    papernum = 0;
    printf("\nInput the student's names in pinyin to search from the list "
           "(Enter to quit):\n");
   
    while(gets(inputname) != NULL && inputname[0] != '\0'){
         
         if(strcmp(papernames[papernum],inputname) == 0)
             printf("\nFind Paper Number: %d.\n",papernum+1);
         
         papernum++;
         printf("\nInput the student's names in pinyin to search from the list "
           "(Enter to quit):\n");
    }
    puts("\nDone!");
    getch();
    return 0;
}

请各位看看问题出现在哪里,谢谢!

最佳答案

查看完整内容

几点建议1 尽量不用gets, 没有对缓冲overrun(覆盖,溢出)做检查2 #define NMLEN 3 char papernames[STUNUM][NMLEN], 如果最长3个首字母时,应该写成 char papernames[STUNUM][NMLEN+1]; 或者增大NMLEN的值。3 比较时papernum递增,没有遍历papernames。也就是第一次查询只和papernames[0]做了比较,第2次查询只和papernames[1]做了比较[ 本帖最后由 ynchnluiti 于 2009-1-17 02:23 编辑 ]

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
发表于 2009-01-17 01:00 |显示全部楼层
几点建议
1 尽量不用gets, 没有对缓冲overrun(覆盖,溢出)做检查
2 #define NMLEN 3
   char papernames[STUNUM][NMLEN],
   如果最长3个首字母时,应该写成
  char papernames[STUNUM][NMLEN+1];
  或者增大NMLEN的值。
3 比较时papernum递增,没有遍历papernames。也就是第一次查询只和papernames[0]做了比较,第2次查询只和papernames[1]做了比较

[ 本帖最后由 ynchnluiti 于 2009-1-17 02:23 编辑 ]

论坛徽章:
0
发表于 2009-01-17 02:20 |显示全部楼层
你读入人名的代码 gets(papernames[papernum]) 有问题,导致了输入的人名并不是你想象中的那样。比如:

输入

zhang
liu
wang
zhao

papername数组的内容实际上是

papername[0]: "zhaliuwanzha"
papername[1]: "liuwanzha"
papername[2]: "wanzha"
papername[3]: "zha"

结果自然出现了找不到对应的人名的情况。

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
发表于 2009-01-17 02:21 |显示全部楼层

  1. /*Find the pinyin initials*/

  2. #include <stdio.h>
  3. #include <string.h>

  4. #define STUNUM 60
  5. #define NMLEN  32

  6. int main(void){

  7.     char papernames[STUNUM][NMLEN], inputname[NMLEN];
  8.     int papernum = 0;
  9.     int i;

  10.     printf("\nInput students' names in pinyin ( Input Enter at the beginning of "
  11.             "a line to quit):\n");
  12.     while(papernum < 60 && gets(papernames[papernum]) != NULL &&
  13.             papernames[papernum][0] != '\0')
  14.         papernum++;

  15.     printf("\nInput the student's names in pinyin to search from the list "
  16.             "(Enter to quit):\n");

  17.     while(gets(inputname) != NULL && inputname[0] != '\0'){
  18.         for (i=0; i<papernum;i++) {
  19.             if(strcmp(papernames[i],inputname) == 0) {
  20.                 printf("\nFind Paper Number: %d.\n",i+1);
  21.                 break;
  22.             }
  23.         }
  24.         printf("\nInput the student's names in pinyin to search from the list "
  25.                 "(Enter to quit):\n");
  26.     }
  27.     puts("\nDone!");
  28. //    getch();
  29.     return 0;
  30. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP