1960 views|0 replies

3836

Posts

19

Resources
The OP
 

How to exit while (cin>>str) loop in C++ [Copy link]

Method: After entering the string, press Ctrl+z on the new line and then press Enter. If you enter data and then press Ctrl+z and then press Enter, it will not work. Reason: The input buffer is a line buffer. When you enter a string of characters from the keyboard and press Enter, these characters will first be sent to the input buffer for storage. Every time you press the Enter key, cin.get() will check whether there is readable data in the input buffer. cin.get() will also check whether the Ctrl+Z or Ctrl+D keys on the keyboard are pressed as the end of the stream mark. There are two ways to check: blocking and non-blocking. The blocking check method means that only after the Enter key is pressed will it check whether the Ctrl+Z key combination has been pressed before. The non-blocking style refers to the way to respond immediately after pressing Ctrl+D. If characters have been entered from the keyboard before pressing Ctrl+D, then Ctrl+D acts as a carriage return, that is, these characters are sent to the input buffer for reading. At this time, Ctrl+D no longer acts as a stream terminator. If there is no keyboard input before pressing Ctrl+D, then Ctrl+D is the signal for the end of the stream. In Windows systems, blocking checks for Ctrl+Z are generally used, while in Unix/Linux systems, non-blocking checks for Ctrl+D are generally used. The author is in Windows, so blocking Ctrl+Z is used to mark the end of the stream. This blocking method has a feature: only after pressing the carriage return can it be possible to detect whether Ctrl+Z was pressed before. Another feature is that if there is readable data in the input buffer, Ctrl+Z will not be detected (because there is data to be read, it cannot be considered that the end of the stream has been reached). Another point to know is that Ctrl+Z does not generate a normal ASCII code value, that is, it does not generate a character, so it cannot be stored in the input buffer like other characters entered from the keyboard. After understanding these points, we can explain the question raised by the original poster. After entering abcd^z from the keyboard and pressing Enter, the Windows system handles it like this: Due to the effect of Enter, the previous abcd and other characters are sent to the input buffer (Note: As mentioned above, ^z does not generate any characters, so it will not be stored in the input buffer, and there is no ^z in the buffer). At this time, cin.get() detects that there is already data in the input buffer (so it no longer checks whether there is input of ^z), so it reads the corresponding data from the buffer. If all the data has been read, the input buffer becomes empty again, and cin.get() waits for new input. It can be seen that although ^z is pressed, there are other input characters (abcd) before it, so the stream will not end. Therefore, the condition for the end of the input stream is: there cannot be any character input before ^z (except Enter), otherwise ^z will not end the stream.

This post is from Microcontroller MCU
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list