防止输入错误数据

防止scanf输入错误数据

防止scanf输入错误数据

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
int main()
{
	int a, t, i = 0;
  retry:

	printf("请输入a的值\n");
	if (i++)
		getchar();
	t = scanf("%d", &a);
	if (t)
		printf("输入值正确!\n");
	else
	{
		printf("输入有误!请重新输入!\n");
		goto retry;
	}

	printf("程序结束\n");
}
updatedupdated2020-05-032020-05-03