- 论坛徽章:
- 0
|
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 编辑 ]
|