c++中为什么会无穷输出

来源:百度知道 编辑:UC知道 时间:2024/05/26 12:37:38
#include"stdio.h"
void main()
{
char x;
do
{
printf("Please enter a number:");
scanf("%d",&x);
if(x=='#')
break;
else
if((x>'0')&&(x<'9'))
printf("digit\n");
else
printf("char\n");
}while(x!='#');
}
这个执行结果会导致不停的输出Please enter a number:这句话,为什么呢?
还是不行啊

1、将
scanf("%d",&x);
修改为
scanf("%c",&x);
2、建议在指令
printf("char\n");
后面添加一行指令
fflush(stdin);

试试看:
void main()
{
char x;
do{
printf("Please enter a number:");
scanf("%d",&x);
if((x>'0')&&(x<'9'))
printf("digit\n");
else
printf("char\n");
}while(x!='#');
}