简体   繁体   中英

How the code gets to the next step only when i press the enter key in c++ (Thread)

I saw the following code on the internet as

DWORD qThreadID;
HANDLE hThread = CreateThread(0, 0, ThreadFn, &uiCounter, 0, &qThreadID);

// Loop until the user enters 'q'
char cChar = ' ';
while (cChar != 'q') {
    cout << uiCounter << endl;
    cChar = (char)getchar();
}

how does the keypress event "Enter" works on it? (when i debug it except for the press of "Enter" no other keypress functionality works ) Thanks

getchar() reads from standard in, which is buffered, both in the library and in the OS. The usual OS's won't return from a read on a console device until enter is entered; they support command line editing, and require the enter key to finalize the input.

getchar() reads a single character of input.

However, your terminal likely does line buffering on the input, no input is sent to your program until you hit enter.

因为enter输入一个\\n ,它将被解释为EOF因为命令行会认为这是用户输入的结尾,并且getChar()试图从该流中读取单个字符缓冲区,因此如果您只是按enter而不返回任何内容,在其前面输入字符,请参阅msdn: http : //msdn.microsoft.com/zh-cn/library/5231d02a%28v=vs.71%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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