由于函数scanf(),不做参数类型的匹配检查,因此,用户输入数据类型与格式字符不符合时,编译程序并不给出提示错误信息,然而却导致不能正确输入数据!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <stdio.h> int main(int argc, char* argv[]) { int a,ret; printf("Input a:"); ret=scanf("%d",&a); if(ret==0) //遇到非法字符的输入 { printf("Input data type error!\n"); fflush(stdin);//清除输入缓冲区的错误数据 } else if(ret==EOF)//scanf调用失败 { printf("Input failure occurs!\n"); } else { printf("a=%d\n",a); } return 0; } |
发表评论
难的是和自己保持联系~