getchar()与getch()

r7746521l 2011-04-23 02:37:33
#include <stdio.h>
int main(void)
{
char a;
a=getchar();
switch(a)
{
case 't':
printf("输入第二个字母:");
if(getchar()=='u')
printf("tuesday\n");
else if(getchar()=='h')
printf("thursday");
else
printf("data error");
break;
case 'm':
printf("monday");
break;
default:
printf("date error");
break;
}
}

先输入't'后再输入'u'后直接输出"data error",怎么会这样啊?
...全文
320 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
匠心零度 2011-05-12
  • 打赏
  • 举报
回复
#include <stdio.h>

int main(void)
{
char a;

a=getchar();

switch(a)
{
case 't':
printf("输入第二个字母:");
fflush(stdin);//加这一句就可以了
if(getchar()=='u')
printf("tuesday\n");
else if(getchar()=='h')
printf("thursday");
else
printf("data error");
break;
case 'm':
printf("monday");
break;
default:
printf("date error");
break;
}

return 0;
}
菜鸟1v1 2011-04-29
  • 打赏
  • 举报
回复
首先 你要搞清楚你在键盘上输入的任何一个字符都被保存在缓冲区内,包括回车, getchar 按顺序提取出来的, 也就是说:
比如:你输入 t 、回车
提示输入第二个字母: u、回车
注意getchar的顺序: a=getchar()、 if(getchar()=='u')、else if(getchar()=='h') 实际上getchar运行了三次 : t、 回车、 u 所以输出的就是error
又如果:你输入 t、回车
提示输入第二个字母: h、回车
getchar运行了三次: t、 回车、 h 所以输出是正确的,

这样解释还不懂,那我也没办法了
匠心零度 2011-04-29
  • 打赏
  • 举报
回复
#include <stdio.h>

int main(void)
{
char a;

a=getchar();

switch(a)
{
case 't':
printf("输入第二个字母:");
fflush(stdin);//加这一句就可以了
if(getchar()=='u')
printf("tuesday\n");
else if(getchar()=='h')
printf("thursday");
else
printf("data error");
break;
case 'm':
printf("monday");
break;
default:
printf("date error");
break;
}

return 0;
}
匠心零度 2011-04-29
  • 打赏
  • 举报
回复
#include <stdio.h>
int main(void)
{
char a;
a=getchar();
switch(a)
{
case 't':
printf("输入第二个字母:");
fflush(stdin);//加这一句就可以了
if(getchar()=='u')
printf("tuesday\n");
else if(getchar()=='h')
printf("thursday");
else
printf("data error");
break;
case 'm':
printf("monday");
break;
default:
printf("date error");
break;
}
}
  • 打赏
  • 举报
回复
getch()直接从键盘获取键值,不等待用户按回车,只要用户按一个键,getch()就立刻返回
getchar() 楼主输入的时候是不是要个Enter呢?
匠心零度 2011-04-28
  • 打赏
  • 举报
回复
fflush(stdin);//清除缓存,否则getchar会从缓存中读取字符

getchar和getch的区别:
getchar()有一个int型的返回值.当程序调用getchar()时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中. 直到用户按回车为止(回车字符也放在缓冲区中).getchar()函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕.如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等……
[/Quote]
pcliuguangtao 2011-04-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 r7746521l 的回复:]

为什么输入 h 时会正常显示?
[/Quote]
呵呵,很明显你输错了~_~
pcliuguangtao 2011-04-23
  • 打赏
  • 举报
回复

#include <stdio.h>
int main(void)
{
char a;
char temp;
a=getchar();
switch(a)
{
case 't':
scanf( "%c",&temp ); //“吃掉”回车符
printf("输入第二个字母:");
a=getchar( );
scanf( "%c",&temp );//“吃掉”回车符
if(a=='u') //if-else里面不要两次使用getchar(),每一次都会取字符,不会放回,putchar()
{
printf("tuesday\n");
}
else if(a=='h')
{
printf("thursday");
}
else
printf("data error");
break;
case 'm':
printf("monday");
break;
default:
printf("date error");
break;
}
return 0;

}

r7746521l 2011-04-23
  • 打赏
  • 举报
回复
为什么输入 h 时会正常显示?
staticabc 2011-04-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 bokutake 的回复:]
LS……万一回车之前还有别的呢?
[/Quote]
那就屁掉了呗,O(∩_∩)O哈哈~
辰岡墨竹 2011-04-23
  • 打赏
  • 举报
回复
LS……万一回车之前还有别的呢?
  • 打赏
  • 举报
回复
我觉得还是用scanf(" %c", &ch);这个更好,空一个空格,以防止有回车在缓冲区!
辰岡墨竹 2011-04-23
  • 打赏
  • 举报
回复
标准的清除缓冲区的方法是:
int c;
while ( ( c = getchar() ) != EOF && c != '\n' )
;
或者
scanf ( "%*[^\n]" );
scanf ( "%*c" );
辰岡墨竹 2011-04-23
  • 打赏
  • 举报
回复
getchar使用了stdin标准流输入,是标准的C库函数。getch是Turbo C等编译环境扩展的C函数,是使用键盘中断等比较低级的方式获取输入,不走流。
另外fflush(stdin)不是没有意义,而是行为没有定义。在VC和TC下会起到清除输入缓冲的作用。在GCC下没效果。
pathuang68 2011-04-23
  • 打赏
  • 举报
回复
getchar和getch的区别:
getchar()有一个int型的返回值.当程序调用getchar()时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中. 直到用户按回车为止(回车字符也放在缓冲区中).getchar()函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕.如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取.也就是说,后续的getchar()调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键. getch()与getchar()基本功能相同,差别是getch()直接从键盘获取键值,不等待用户按回车,只要用户按一个键,getch()就立刻返回,getch()返回值是用户输入的ASCII码,出错返回-1.输入的字符不会回显在屏幕上.getch()函数常用于程序调试中,在调试时,在关键位置显示有关的结果以待查看,然后用getch()函数暂停程序运行,当按任意键后程序继续运行.
pathuang68 2011-04-23
  • 打赏
  • 举报
回复
fflush(stdin)是没有意义的。

fflush仅对stdout有效。
bdmh 2011-04-23
  • 打赏
  • 举报
回复
加一句,如下

case 't':
fflush(stdin);//清除缓存,否则getchar会从缓存中读取字符
KID_coder 2011-04-23
  • 打赏
  • 举报
回复
又是缓冲区的问题~
fflush(stdin);getch()清除一下~再getchar()
判断是否等于u时候getchar获取的是回车的ascII码不是u的ascii码
canican 2011-04-23
  • 打赏
  • 举报
回复
a=getchar();在这句话后边加fflush(stdin);

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧