C语言 while (getchar() != '\n' );

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40554617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:31:24  来源:igfitidea点击:

while (getchar () != '\n' );

cfor-loopwhile-loopgetchar

提问by Mondo192

I have the following for loop, I am prompting the user to enter a 4 digit pin and hit enter. Can someone explain to me what the while loop is really doing because I don't fully understand it.

我有以下 for 循环,我提示用户输入一个 4 位数的 pin 并按回车键。有人可以向我解释 while 循环真正在做什么,因为我不完全理解它。

//user input for pin
for(i = 0; i < PIN_LENGTH; i++)
{
    printf("Enter digit %d of your PIN: ", i);
    user_pin[i] = getchar();
    while (getchar() != '\n'); //what is this line doing??
}

回答by ex nihilo

As mentioned by others, this loop discards unwanted characters from stdinso that the next input function has a clean stream, and in particular it discards the \nthat follows the last character entered by the user. But, getchar()returns EOFin the event of a read error, so the loop should test for EOFalso, like this:

正如其他人所提到的,这个循环会丢弃不需要的字符,stdin以便下一个输入函数有一个干净的流,特别是它会丢弃\n用户输入的最后一个字符之后的 。但是,在发生读取错误时getchar()返回EOF,因此循环也应测试EOF,如下所示:

int ch;
while ((ch = getchar()) != '\n' && ch != EOF)
    continue;  // discard unwanted characters

Also note that, if stdinhas been redirected, it is possible to reach EOFwithout encountering a \n. And, as @chqrlie pointed out in the comments, the user can signal an EOFfrom the terminal by entering Ctrl-Din Linux, or Ctrl-Zin Windows. Hence the importance of testing for EOFexplicitly.

另请注意,如果stdin已被重定向,则可以在EOF不遇到\n. 而且,正如@chqrlie 在评论中指出的那样,用户可以EOF通过Ctrl-D在 Linux 或Ctrl-ZWindows 中输入来从终端发出信号。因此,EOF明确测试的重要性。

回答by Some programmer dude

When you give input to the program, then you end it with the Enterkey. This key is sent to your program as a newline.

当您向程序提供输入时,您就以Enter键结束它。此密钥作为换行符发送到您的程序。

What the loop does is to read and discard characters until it has read the newline. It flushes the input buffer.

循环所做的是读取和丢弃字符,直到它读取了换行符。它刷新输入缓冲区。

If you don't do this, then the next main getcharcall would return the newline instead of the digit you expect.

如果您不这样做,那么下一个主getchar调用将返回换行符而不是您期望的数字。

回答by Jean-Fran?ois Fabre

The next line is discarding the possible extra chars that the user may have inputted, and also the linefeed char that the user hadto input.

下一行丢弃可能的额外字符,用户可能已经输入,并且还换行字符,用户必须输入。

So other scanf/getcharmethods further in the code are not polluted by this spurious input.

因此scanf/getchar,代码中的其他方法不会受到这种虚假输入的污染。

回答by CAW

**The function of this while loop is to clear the users' illegal input .When the input is '\n' ,the loop ends.

**这个while循环的作用是清除用户的非法输入。当输入为'\n'时,循环结束。

回答by Amal Dev

The while loop is used to check when the user will press enter.Enter is considered as '\n'.

while 循环用于检查用户何时按下 Enter。Enter 被视为 '\n'。

回答by lorenzo merici

That line is a while, so at run-time it will repeat it until getchar() == '\n'. '\n' is a character that in ASCII means "enter" key. In this way you can test that the user will not press "enter" without had inserted a number.

该行有一段时间,所以在运行时它会重复它直到 getchar() == '\n'。'\n' 是一个字符,在 ASCII 中表示“回车”键。通过这种方式,您可以测试用户在没有插入数字的情况下不会按“输入”。