cin can not get entered after the Ctrl + Z or Ctrl + D

I wrote a small program a digital count, I will enter a bunch of numbers, and then enter a numeric input is completed to be searched
but after I finished entering the number set by pressing ctrl + d, the program is immediately terminated, and did not in the second input in place of stay, I checked the internet search and found a lot of people run into this problem
solving method first see is that after the completion of adding an input

cin.clear();

But I used to no avail, you can see a plus

cin.sync();

The same is not used after effect, before a role is the wrong state to change to the active state, after a buffer is cleared of unread messages, supposedly this is possible, and I read through cin.rdstate (), can be We can see restored to 0, but the problem is still not resolved, continue to find information, but also find a way to add the following two

cin.clear();
clearerr(stdin);

Then the problem is solved

After find later found that the above solution is to solve the code windos platform, and I was under Linux, you must use the following methods
for the following reasons:
cin method detects the EOF, will set the cin object indicating EOF condition mark. After setting this flag, cin will not be called again cin reads the input does not work ... cin.clear () method may clear the EOF mark, the input to proceed. But remember that, in some systems, by crl + z / ctrl + d is actually the end of the input and output, and cin.clear () will not be able to recover input and output.
And why add the same code, on Windows you can, you can not do on my Mac?

The input buffer is a line buffer. After entering the string of characters from the keyboard and press Enter, these characters are first stored in the buffer to the input. Every time the Enter key is pressed, cin.get () will detect whether the input data buffer has been read. cin.get () will also check is made as to whether there is flow mark the end of the keyboard Ctrl Z or Ctrl + D + key is pressed, it checks in two ways: blocking and non-blocking.

Blocking check mode refers to the Enter key is pressed only after fishes have had if Ctrl + Z key combination is pressed check pattern refers to a non-blocking mode immediately after the press Ctrl + D response. If before pressing Ctrl + D characters has been input from the keyboard, the Ctrl + Enter action is equivalent to D, i.e., the input buffer for these characters to read, in which case Ctrl + D no longer play stream terminator role. If there is no keyboard input before pressing Ctrl + D, then Ctrl + D is the end of the current signal.

Windows systems generally use blocking the inspection Ctrl + Z, Unix / Linux systems generally use non-blocking check Ctrl + D. Blocking has a characteristic: only after pressing Enter will it be possible to detect whether or not there before pressing Ctrl + Z. Another feature: if the input buffer has not detected the data readable Ctrl + Z (as the data to be read, can not be considered to end of the stream). Another point that needs to know: Ctrl + not an ordinary ASCII value Z produced, that is not a character that it produces, so it will not with other characters entered from the keyboard as can be stored in the input buffer.

And what role is it clearerr:
refer to the official document: address
to simply say that it can reset the state of the stream, which is the need to reset the parameters of flow. The cin.clear () does not guarantee that you can reset the stream,
the Clear official document: Address

Published 123 original articles · won praise 610 · Views 350,000 +

Guess you like

Origin blog.csdn.net/a568713197/article/details/104323488