为什么会这样呢?
鬼火狼烟吧
全部回复
仅看楼主
level 3
ellison909 楼主
#include "stdafx.h"int Number=0;int _tmain(int argc, _TCHAR* argv[]){ TCHAR a;while(1){scanf("%c",&a);switch(a){case 'a':case 'A':Number++;break;case 'd':case 'D':Number--;break;case 's':case 'S': printf("The Number is %d.\n",Number);break;case 'o':case 'O': break;default: printf("Wrong command.\n");}if((a=='o')||(a=='O'))break;}getchar();return 0;}运行后,为什么我输入a,会打出Wrong command.??如下:aWrong command.sThe Number is 1.Wrong command.好象是输入函数的地方出现了问题。大哥,这是为什么呢?
2007年05月01日 03点05分 1
level 3
ellison909 楼主
对了,我用的是VS 2003。Debug和Release版exe的运行结果都是一样的,和我预期的不一样为什么呀?
2007年05月01日 03点05分 2
level 9
加个判断就知道怎么回事了呵呵:case '\n': case '\r': printf("Your input is enter~.\n"); break;
2007年05月02日 13点05分 3
level 9
解决这个的办法有2个:一个是用下面的办法,不过只限在windows下:scanf("%c\n",&a);另一个,不限制操作系统:scanf("%c",&a); getc();
2007年05月02日 13点05分 4
level 3
ellison909 楼主
为什么会产生这样的现象呢?
2007年05月03日 04点05分 5
level 9
主要是你输入的字符其实不是1个,而是2个,1个是你的字母,另一个是你的确认的回车符,而scanf是持续接受键盘缓冲区的内容的,缓冲区中最后的内容是回车,所以需要有一个取字符的函数比如getc()过滤掉这个回车,否则回车会被scanf接受到。
2007年05月07日 01点05分 6
1