Chinaunix

标题: [讨论]是strcmp的问题还是其他地方出错? [打印本页]

作者: mcmay    时间: 2009-01-17 01:00
标题: [讨论]是strcmp的问题还是其他地方出错?
下面的代码是让用户输入一组人名汉语拼音的首字母组合并存入一个二维字符串数组,然后让他再单独输入一个人名汉语拼音首字母组合(一维字符串),看看这个字母组合在不在刚刚输入的那组二维字符串数组中,如果在就输出该拼音在二维字符串数组中的位置。结果出现了找不到输入的正确的拼音字符串的问题。

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

请各位看看问题出现在哪里,谢谢!
作者: ynchnluiti    时间: 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 编辑 ]
作者: asmcer    时间: 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"

结果自然出现了找不到对应的人名的情况。
作者: ynchnluiti    时间: 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. }
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2