69,373
社区成员
发帖
与我相关
我的任务
分享
//请今后要用
char c;
scanf("%c",&c);
//时,都改为
char s[2];
char c;
scanf("%1s",s);
c=s[0];
//自动跳过一个或多个空格或Tab字符或回车换行,读取下一个字符。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
int main()
{
char word[80],**p,ch;
char *name[]={"liuxiaoyang","shihaoren","jixiaofei","shihuaidan","yang","shuaige","fei","huaidan",NULL};
do
{
printf("please enter words: ");
scanf("%s", word);
p = name;
do
{
if(!strcmp(*p, word))
{
puts("the meaning of the word is: ");
puts(*(p+1));
break;
}
if(strcmp(*p,word)!=0)
p = p + 2;
}while((*p)!=NULL);
if((*p)==NULL)
{
printf("%s","the word is not in the dictioary");
printf("\n");
}
printf("enter y/n:");
getchar(); /*接收上面残留的输入缓冲区的回车符*/
scanf("%c", &ch);//该句无法执行
}while(toupper(ch)!='N');
system("pause");
return 0;
}
在scanf("%c", &ch);上面加一句getchar();代码即可。详见上面的代码;
另外,注意你的逻辑可能会出现越界访问,导致段错误;
if(strcmp(*p,word)!=0)
p = p + 2;
输入的word一直不和*p相同,则p会一直向后偏移。