标签:10 scanf 函数调用 while printf Data bug 输入 getchar
【C语言】关于解决scanf函数调用中的恶意输入/无效输入导致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");
}
}
标签:10,scanf,函数调用,while,printf,Data,bug,输入,getchar 来源: https://blog.csdn.net/weixin_50750441/article/details/115194184
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。