简体   繁体   English

while语句中的无限循环

[英]Infinity loop in while statement

Here is a snippet from my code 这是我的代码片段

scanf("%d", &s);

while(s!=1 && s!=2) {
    if(scanf("%d", &s) != 1) {
        show_warning(); //just print some info msg
    }
}

The idea is to execute show_warning function only if user enter something different of 1,2 and entered value to be only integer. 想法是仅当用户输入的值不同于1,2,并且输入的值只能是整数时,才执行show_warning函数。 With the code above it jumps in infinity loop. 使用上面的代码,它会跳入无限循环。 How to fix that ? 如何解决?

The problem is that the failed input operation doesn't extract any characters from the stream (and you'll keep reading the invalid input over and over), so you have to empty the input manually. 问题在于,失败的输入操作不会从流中提取任何字符(并且您将一遍又一遍地读取无效的输入),因此您必须手动清空输入。 For example: 例如:

char buf[1000];

// ...

if(scanf("%d", &s) != 1)
{
    puts("Error, try again: ");
    fgets(buf, 1000, stdin);
}

As I suggested in the other question , if you use fgets from the start to always read one line and process it later, you overcome this problem. 正如我在另一个问题中建议的那样,如果从一开始就使用fgets始终读取一行并在以后进行处理,则可以解决此问题。

(The same philosophy is true in C++: read the whole line first so the input stream can move on, and process the line later to see if its valid.) (在C ++中也是如此:首先读取整行,以便输入流可以继续前进,然后再处理该行以查看其是否有效。)

Why are you using a while loop? 为什么要使用while循环? Do you want show_warning(); 您是否要show_warning(); to be called once, or repeatedly? 一次或多次被叫? If you answered once then you only need an if-statement. 如果您回答一次,则只需要一个if语句。

Also what happens when no scanf succeeded? 如果没有scanf成功,还会发生什么? The variable s remains unchanged (and perhaps with an undefined value, if you did not initialize it). 变量s保持不变(如果未初始化,则可能具有未定义的值)。 You should set it, or change the condition of the while ! 您应该设置它,或更改while条件!

And you really should learn to compile with warnings enabled and debugging information (ie gcc -Wall -g on Linux) and to use a debugger (eg gdb on Linux) 而且,您确实应该学会使用已启用的警告和调试信息进行编译(例如,在Linux上为gcc -Wall -g )以及使用调试器(例如在Linux上为gdb

What makes you think it is looping until infinity? 是什么让您认为循环一直循环到无穷远?

You only get the warning message when you have not entered a number because it did not read a token. 仅当您没有输入数字时,您才会收到警告消息,因为它没有读取令牌。

Changing the && to || 将&&更改为|| though is a certain way to ensure it will run to infinity as your loop will never break then. 不过,这是确保其运行到无穷大的某种方法,因为您的循环将永远不会中断。

You might want an alternative message to prompt the user to enter data when they did enter a number but not 1 or 2? 您可能想要一条替代消息来提示用户输入数字而不是1或2时输入数据?

I'm assuming you're having issues when a non-integer is input. 我假设您在输入非整数时遇到问题。 This is because scanf leaves non-matching characters in the buffer, so subsequent calls are seeing the same input and looping. 这是因为scanf在缓冲区中scanf不匹配的字符,因此后续调用将看到相同的输入和循环。

What you need to do is if the scanf call returns any number other 1 is: read that character in using scanf("%c",&somechar) so you can print it and tell the user that it isn't accepted. 您需要做的是,如果scanf调用返回的其他数字不是1,那就是:使用scanf("%c",&somechar)读取该字符,以便您可以打印它并告诉用户它不被接受。 The non-accepted input will then have been removed so the next call to scanf will give you the next input rather than the one you saw on the previous iteration. 然后将删除不接受的输入,因此对scanf的下一次调用将为您提供下一个输入,而不是您在上一次迭代中看到的输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM