在学习c primer plus 的时候发现处理错误输入的方法有两种
第一种:使用scanf("%*s");
#include<stdio.h>
int main(void)
{
int i = 0;
while (scanf("%d",&i)!=1)
{
scanf("%*s");//第一种方法
printf("请再次输入\n");
}
return 0;
}他的处理结果是这样的<img src="https://img-blog.youkuaiyun.com/20150118151636827?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhbmcxMjN6aGFuZ3lhbw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />第二种:<pre name="code" class="cpp">#include<stdio.h>
int main(void)
{
int i = 0;
while (scanf("%d",&i)!=1)
{
while (getchar()!='\n')//第二种
{
continue;
}
printf("请再次输入\n");
}
return 0;
}他的处理结果是这样的看出来不一样了吗?使用第一种的话,代码会更简洁,第二种的话结果会更简洁
在学习《C Primer Plus》时,遇到了处理错误输入的两种方法。第一种方法是利用`scanf("%*s");`来跳过错误的输入。文章探讨了这种方法与其他错误输入处理策略的差异。
731

被折叠的 条评论
为什么被折叠?



