【C语言】关于解决scanf函数调用中的恶意输入/无效输入导致bug问题以及代码实现自动化解决解析

【C语言】关于解决scanf函数调用中的恶意输入/无效输入导致bug问题以及代码实现自动化解决解析

以往输入字符的时候总是是因为前方有输入导致scanf("%c")“没法”输入,甚至有限的getchar()都无法避免,为此设计出了一种利用scanf()和getchar()函数性质来解决输入失败或者输入bug/不美观的方案

这里输代码:

#include <stdio.h>

int FormatInput(char opt);

int FormatInput(char opt)
{
	while (1)
	{
		if (opt == 'Y' || opt == 'N')
		{
			if(opt == 'Y')
			{
				return 1;
				break;
			}
			else
			{
				printf("\n\n\n\n\n\n");
				return 0;
				break;
			}
		}
		else
		{
			while(getchar() != 10);// delete meaningless strings
			printf ("\n=============================\n       INVALID DATA ! \n=============================\n");
			printf("Enter valid data (Y or N) to make your choice.\n");
			printf("\nYour choice:");
			scanf("%c",&opt);
		}
	}
}

int main (void)
{
	int num1,num2,num3;
	double average;
	int i,j,k;
	int check;
	char opt;
	
	printf("(Enter any character to start.)");
	
	/* get the input data and provide chances to check and reenter data*/
	do
	{
		i=j=k=0;
		while(i == 0)
		{
		    while(getchar() != 10);
			printf("Enter 1st integer numbers: ");
			i = scanf("%d",&num1);
			while(getchar() != 10);
			if(i == 0) printf ("\n=============================\n       INVALID DATA ! \n=============================\n\n");
			else
			{
				printf("%c",7);// prompt sound rings when enter finished
				printf("\n=========== Data has been successfully entered! ===========\n");
				printf("\nThe 1st number is %d.\n",num1);
				printf("\n=========== Data has been successfully entered! ===========\n");
				printf("\n\n\n");
			}
		}
		while(j == 0)
		{
			while(getchar() != 10);
			printf("Enter 2nd integer numbers: ");
			j = scanf("%d",&num2);
			if (j == 0) printf ("\n=============================\n       INVALID DATA ! \n=============================\n\n");
			else
			{
				printf("%c",7);// prompt sound rings when enter finished
				printf("\n=========== Data has been successfully entered! ===========\n");
				printf("\nThe 2nd number is %d.\n",num2);
				printf("\n=========== Data has been successfully entered! ===========\n");
				printf("\n\n\n");
			}
		}
		while(k == 0)
		{
			while(getchar() != 10);
			printf("Enter 3rd integer numbers: ");
			k = scanf("%d",&num3);
			if (k == 0) printf ("\n=============================\n       INVALID DATA ! \n=============================\n\n");
			else
			{
				printf("%c",7);// prompt sound rings when enter finished
				printf("\n=========== Data has been successfully entered! ===========\n");
				printf("\nThe 3rd number is %d.\n",num3);
				printf("\n=========== Data has been successfully entered! ===========\n");
				printf("\n\n\n");
			}
		}
		while(getchar() != 10);// delete meaningless strings
		//getchar();//delete \n
		printf("%c",7);// prompt sound rings when enter finished
		printf("\n=========== Data has been successfully entered! ===========\n");
		printf("\nThe 1st number is %d.\nThe 2nd number is %d.\nThe 3rd number is %d.\n",num1, num2, num3);
		printf("\n=========== Data has been successfully entered! ===========\n");
		printf("\nAre these numbers what you need?");
		printf("(Y or N)\n");
		printf("\nYour choice:");
		scanf("%c",&opt);
		check=FormatInput(opt);
	}while (check == 0);
	
	printf("\n\n=========== Data enter finished! ===========\n\n");
	printf("%c",7);
	
	/* calculate the average */
	average = (num1 + num2 + num3) / 3.0;
	
	/* display the result */
	printf("\nThe average of %d, %d and %d is %f\n\n",num1, num2, num3, average);
	
	while(getchar() != 10);
	while(getchar() == 10);
	getchar();
	
	return 0;
}

以下是自动化实现的详细解析

  • 用户输入时恶意/无意输入除数字外等一切符号:

字母和特殊字符:如果仅仅含有scanf函数输入,字母和特殊字符的额外输入必然导致程序直接退出,于是利用scanf()也为int函数,其返回值是成功输入的数据的个数的特性,可以先判断scanf("%d",&num1) ==1?,防止输入失败的连带程序自我了结,并利用while(getchar() != 10)消除多余字符。

空格,字母和特殊字符:空格尽管不会导致程序退出,但他们的溢出也会对后面的输入%c和有限的getchar()造成恶意输入而产生未知的bug,通过**while(getchar() != 10)**配合getchar()实现完全消除恶意输入,此时不再担心因为在有机可乘的地方乱输入而导致输入Y/N时出现bug——连续执行n次每输入n个非法字符,提供良好的交互式体验
注:getchar()为int函数,可以返回所输入字符的ASCII码,只要不为10(换行符的ASCII码)就继续getchar()消除恶意溢出字符

while(i == 0)
		{
		    while(getchar() != 10);
			printf("Enter 1st integer numbers: ");
			i = scanf("%d",&num1);
			while(getchar() != 10);
			if(i == 0) printf ("\n=============================\n       INVALID DATA ! \n=============================\n\n");
			else
			{
				printf("%c",7);// prompt sound rings when enter finished
				printf("\n=========== Data has been successfully entered! ===========\n");
				printf("\nThe 1st number is %d.\n",num1);
				printf("\n=========== Data has been successfully entered! ===========\n");
				printf("\n\n\n");
			}
		}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值