scanf函数和循环 [英] Scanf and loops

查看:110
本文介绍了scanf函数和循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是遍地应该循环,直到用户一张code的输入0至15之间的数字。

here a a piece of code that is supposed to loop over and over until the user inputs a number between 0 and 15

        int input;

        do
        {

                printf("Enter a number => ");
                scanf("%d",&input);
                printf("\n %d \n",input);

        }
        while (!(input>0 && input <15));

但是,如果用户放入类似gfggdf它导致循环重复一遍又一遍又一遍,从来没有再次提示用户输入...控制台看起来像这样

However if the user puts in something like "gfggdf" it results in the loop repeating over and over and over and never prompting the user for input again... the console looks like this

Enter a number =>
0
Enter a number =>
0
Enter a number =>
0
Enter a number =>
0
Enter a number =>
0
Enter a number =>
0
(looping indefinitely)

通过这本书细算,看来我需要做这样的事情,以prevent这种情况的发生。

After looking through this book, it seems I need to do something like this to prevent this from happening.

int input, error, status;
char skip_ch;

do
{
        error = 0;

        printf("Enter a number => ");
        status = scanf("%d",&input);
        printf("\n %d \n",input);

        if(status!=1)
        {
                error=1;
        }
        else if(input < 0 || input >15){
                error = 1;
        }

        do
        {
                scanf("%c",&skip_ch);
        }while(skip_ch != '\n');
}
while (error);

我明白需要检查scanf函数的状态,以确保它是有效的投入,我不明白的内部do-whil​​e循环。我觉得这本书从来没有向我解释为什么scanf函数需要调用几次这样,就好像不知何故scanf的缓冲得到摆满了一堆乱七八糟的东西,它以某种方式清理出来,通过它一百万次只是循环你。

I understand needing to check for the scanf status to make sure it was valid input, What I don't understand the the inner do-while loop. I feel like the book never really explained to me why scanf needs to be called several times like that, it's like somehow the scanf buffer got filled up with a bunch of garbage and it's somehow cleaning it out by just looping through it a million times for you.

谁能解释一下这个黑魔法我?

Could anyone please explain this black magic to me?

推荐答案

原来code循环下去,因为无效的数据(以下简称gfggdf)是的的从输入缓存中删除scanf的失败将其转换成一个整数 - 它留在输入缓冲器,所以scanf的下一个呼叫着眼于相同的数据,和(当然)仍然不能将其转换为整数,因此该循环再次执行,结果仍然没有改变。

The original code loops indefinitely because the invalid data (the "gfggdf") is not removed from the input buffer when scanf fails to convert it to an integer -- it's left in the input buffer, so the next call to scanf looks at the same data, and (of course) still can't convert it to an integer, so the loop executes yet again, and the results still haven't changed.

do {
        scanf("%c",&skip_ch);
}while(skip_ch != '\n');

简单地一次读取一个字符(并忽略它),直到它到达该线的末端。如果preFER获得无环路消除输入缓冲区的垃圾,你可以使用类似: scanf函数(%* [^ \\ n]);

广泛使用这(可能更多),另一种可能性是通过阅读一整行,然后试图转换什么在该行启动。无论是转换与否,您已经阅读所有的数据到行的末尾,以便在读取下一个尝试/转换将从下一行开始转换是否有效与否。这确实有一个潜在的弱点:如果你有一个真正的的输入线,你可能最终会读取和存储了很多,而您并没有真正使用数据。这是很少什么大不了的事,但你仍然应该意识到这一点。

Another possibility that's (probably more) widely used is to start by reading an entire line, then attempting to convert what's in the line. Whether it converts or not, you've already read all the data to the end of the line so the next attempt at reading/converting will start from the next line whether the conversion worked or not. This does have one potential weakness: if you got a really long line of input, you could end up reading and storing a lot of data for which you have no real use. It's rarely a big deal, but you should still be aware of it.

这篇关于scanf函数和循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆