| |

VerySource

 Forgot password?
 Register
Search
View: 1008|Reply: 9

On the issue of while (cin >> str) exit!

[Copy link]

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-3 23:50:01
| Show all posts |Read mode
#include <iostream>
#include <string>

using namespace std;

int main ()
{
string line;

while (cin >> line)
cout << line << endl;
Ranch
return 0;
}
 The program is like this, I can't launch by directly typing ctrl + Z.
Why do you need to enter it twice to exit?
Can anyone help explain it in detail?
Thank you, brother!
Reply

Use magic Report

0

Threads

19

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-4 01:21:01
| Show all posts
Yes,
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 Invalid IP Address

Post time: 2020-1-4 01:54:01
| Show all posts
You must enter CTRL + Z separately after entering a new line, otherwise cin >> line will not return 0.
Reply

Use magic Report

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-4 03:21:01
| Show all posts
Why?
I do n’t understand, not to say that you should return after reading eof!
Reply

Use magic Report

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-4 04:21:01
| Show all posts
At any time, this program must enter ctrl + z twice to exit.
How did my first ctrl + Z program handle it?
Reply

Use magic Report

0

Threads

19

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-4 05:24:01
| Show all posts
Let's see how io is designed.
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-1-4 06:12:01
| Show all posts
Is there a way to append a parameter, such as '\n' to end?
Reply

Use magic Report

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-4 09:03:01
| Show all posts
Come to give points in the evening, I will read the book again.
Reply

Use magic Report

1

Threads

5

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-4 10:39:01
| Show all posts
After searching for a long time on the Internet, I finally found the answer, thank you whyglinux (山青水秀).
Here is the original text:
The reasons are as follows:

The input buffer is a line buffer. When you enter a string of characters from the keyboard and press Enter, these characters are first sent to the input buffer for storage. Whenever the enter key is pressed, cin.get () detects if there is readable data in the input buffer. cin.get () will also check if there is a Ctrl + Z or Ctrl + D key on the keyboard as the end of stream sign. There are two ways to check: blocking and non-blocking.

The blocking check method refers to checking whether there is a Ctrl + Z key combination pressed before pressing the Enter key. The non-blocking check method refers to a method of responding immediately after pressing Ctrl + D. If characters have been entered from the keyboard before pressing Ctrl + D, the effect of Ctrl + D is equivalent to a carriage return, that is, these characters are sent to the input buffer for reading. At this time, Ctrl + D no longer serves as the end-of-stream character. Role. If there is no keyboard input before pressing Ctrl + D, Ctrl + D is the signal for the end of the stream.

Windows systems generally use blocking Ctrl + Z, and Unix / Linux systems generally use non-blocking checking Ctrl + D. The host is under Windows, so use blocking Ctrl + Z to mark the end of the stream.

This blocking method has a feature: it is only possible to detect if there was a Ctrl + Z press before pressing Enter. 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, the end of the stream cannot be considered yet). One more thing to know: Ctrl + Z does not produce an ordinary ASCII value, which means that it does not produce 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 questions raised by the landlord.

After entering abcd ^ z from the keyboard and entering the carriage return, it is processed as follows on the Windows system: due to the effect of the carriage return, the characters such as abcd are sent to the input buffer (note: as mentioned above, ^ z will not produce Characters, so it will not be stored in the input buffer, there is no ^ z in the buffer). At this point, cin.get () detects that there is already data in the input buffer (so it no longer checks for ^ z input) and reads the corresponding data from the buffer. If they are all read, the input buffer becomes empty again, and cin.get () waits for new input. It can be seen that although there is ^ z pressed, because there are other input characters (abcd) before this, the stream will not end.

Therefore, the condition for the end of the input stream is that no characters can be entered before the ^ z (except carriage return), otherwise ^ z will not play the role of the end of the stream.
I will post it in half an hour. I hope you all have a look at this question!
Reply

Use magic Report

0

Threads

36

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-8 13:54:02
| Show all posts
mark
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list