清除缓存区的错误数据

处理非法数据

常常用于清除缓存区的字符的语法

while(getchar()!='\n');/*清除缓存区的非法字符*/
fflush(stdin);/*清除缓存区的错误数据*/

问题:输入两个整型数,计算并输出两个整数的最大值。如果用户不慎输入了非法字符,那么程序可提示“‘输入数据个数或格式错误”
这个简单程序就用到了fflush

#include
main()
{
	int a,b,max,ret;
	printf("input a,b:");
	ret=scanf("%d,%d",&a,&b);
	if(ret!=2)
	{
		printf("input data quantity or format error!\n");
		fflush(stdin);
	}
	else
	{
		max=a>b?a;b;
		printf("max=%d\n",max);
	}
}
一个能猜十次的猜数小程序代码

这之中就用到了whlie(getchar()!=’\n’)

#include
#include
#include
main()
{
	int magic,guess,counter;
	int ret;
	srand(time(NULL));
	magic=rand%100+1;/* magic取随机数*/
	do{
		printf("please guess a magic number:");
		ret=s

你可能感兴趣的:(学习笔记,C语言中清除缓存区数据)