简体   繁体   English

cin.get() 的空输入到底意味着什么?

[英]What exactly empty input means for cin.get()?

I think it's a simple question, but I don't understand the concept in this sample of code, mainly in the while loop:我认为这是一个简单的问题,但是我不明白这个代码示例中的概念,主要是在while循环中:

#include <iostream>

const int ArSize = 10;
void strcount(const char * str);

int main(){
    using namespace std;
    char input[ArSize];
    char next;


    cout << "Enter text:\n";
    cin.get(input, ArSize);
    while(cin){

        cin.get(next);
        while(next != '\n')
            cin.get(next)
        
        strcount(input);
        cout << "Enter next line, empty line ends the program:\n";
        cin.get(input, ArSize);
    }

    cout << "The end\n";
    return 0;
}

...

What I understand is that the while loop continues until cin returns false.我的理解是while循环一直持续到cin返回false。 It filters out the remaining input that's left in the buffer (because it wasn't the size of ArSize or under, or it was - then it will just filter out the newline character) until it meets the newline character.它过滤掉留在缓冲区中的剩余输入(因为它不是 ArSize 或以下的大小,或者它是 - 然后它只会过滤掉换行符),直到它遇到换行符。 Then it counts string's characters (not important in this question), and then, let's say someone just presses enter.然后它计算字符串的字符(在这个问题中并不重要),然后,假设有人只是按下回车键。 cin.get() discards newline character in input. cin.get()丢弃输入中的换行符。 So if someone for example enters an empty line of text in the terminal, it reads it as 'failed' input and cin returns false?因此,例如,如果有人在终端中输入了一个空文本行,它会将其读取为“失败”输入,而cin返回 false? Because if someone proceeds to the new line, just by pressing enter, it just leaves the newline character in the buffer, and cin.get() can't get it so it returns false.因为如果有人进入新行,只需按 enter,它只会将换行符留在缓冲区中,而cin.get()无法获取它,因此它返回 false。 Or am I wrong?还是我错了?

In short - What exactly happens if you just press enter?简而言之 - 如果您只是按 Enter 键会发生什么? cin.get() can't get the input because there's only newline in buffer and it counts it as failed input, so it returns false? cin.get()无法获取输入,因为缓冲区中只有换行符并将其视为失败输入,所以它返回 false?

If cin.get(input, ArSize);如果cin.get(input, ArSize); reads no characters (ie the first character it encounters is a newline) it calls setstate(failbit) putting the stream into a failed state and therefore while(cin) becomes false ending the loop.不读取任何字符(即它遇到的第一个字符是换行符)它调用setstate(failbit)将 stream 放入失败的 state 中,因此while(cin)成为错误的结束循环。

As you can see here from the CPP reference https://en.cppreference.com/w/cpp/io/basic_istream/get正如您从 CPP 参考https://en.cppreference.com/w/cpp/io/basic_istream/get中看到的那样

cin.辛。 get() is used to read the next character from the keyboard buffer and it returns that character in case it was available to be read and returns EOF otherwise and sets failbit and eofbit (which makes the expression in the if statement evaluates to false). get() 用于从键盘缓冲区读取下一个字符,如果该字符可供读取,则返回该字符,否则返回 EOF 并设置 failbit 和 eofbit(这使得 if 语句中的表达式计算为 false)。

now let's see the code in action line by line:现在让我们逐行查看代码:

while(cin){

this evaluates to true as long as the failbit flag in the cin object is set to goodbit showing no error.只要 cin object 中的failbit标志设置为goodbit显示没有错误,这就会评估为 true。 ( https://en.cppreference.com/w/cpp/io/ios_base/iostate ) https://en.cppreference.com/w/cpp/io/ios_base/iostate

 cin.get(next);
     while(next != '\n')
          cin.get(next)

the first line reads the next character from the keyboard buffer and stores it in the next variable and the while loop checks for the newline character which is equivalent to pressing Enter if it is not the next character in the buffer then continue reading and storing in next until it meets a newline character then it exits the loop returning to the outer while loop.第一行从键盘缓冲区中读取下一个字符并将其存储在 next 变量中,while 循环检查换行符,如果它不是缓冲区中的下一个字符,则相当于按Enter ,然后继续读取并存储在下一个直到它遇到一个换行符然后它退出循环返回到外部while循环。

strcount(input);
    cout << "Enter next line, empty line ends the program:\n";
    cin.get(input, ArSize);

then strcount function as I assume is used to count the characters entered by the user in the input array by this line of code before the while loop.然后我假设的 strcount function 用于计算用户在while循环之前通过这行代码在输入数组中输入的字符。

cin.get(input, ArSize);

and then at the last line inside of the while loop, the program reads another input by the user.然后在 while 循环的最后一行,程序读取用户的另一个输入。

Please Note:请注意:

the use of these three lines here is to make sure that each line is read at every single loop with no characters read in the second input before the newline character appears even if the number of characters is bigger than the ArSize variable.此处使用这三行是为了确保在每个循环中读取每一行,并且在出现换行符之前在第二个输入中没有读取任何字符,即使字符数大于ArSize变量也是如此。 when that happens the first line before the while loop will read the number of ArSize from the buffer and if there are remaining characters other than the newline it will be read by the three lines until a newline appears so that the next to get function to read the input will start looking for characters in the buffer after the previous newline.当发生这种情况时,while循环之前的第一行将从缓冲区中读取ArSize的数量,如果除了换行符之外还有剩余的字符,它将被三行读取,直到出现换行符,以便下一个让function读取输入将在前一个换行符之后开始在缓冲区中查找字符。

cin.get(next);
    while(next != '\n')
        cin.get(next)

if there is anything unclear please let me know.如果有任何不清楚的地方,请告诉我。

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

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