C++ Issue with Cin and Ctrl + Z

C++ Issue with cin and CTRL + Z

The ^Z has to be first in order for Windows to treat it as Ctrl+Z, otherwise it is just treated as meaningless characters.

If you would like it to work like you wrote i'd suggest:

String wordBuffer("")
while (strcmp(wordBuffer[strlen(wordBuffer)-3], "^Z") != 0){
words.push_back(wordBuffer);
cin >> wordBuffer
}

EDIT: in your second example it works because when you read integers c++ knows to divide the given string of numbers in the space (or ENTER if the numbers are entered separately in every line) to read every number separately so if you'll enter:

123 2323 4545 43 ^Z

It will read 123, then 2323, ... and then ^Z and so it will be as though it got it in a separate line but when you read string, it cant do that because a string contain every symbol and so it separate the input in the ENTER pressed and that why the second one works

C++ getline(cin, buffer) doesn't stop when I press CTRL+Z

As explained by OP: This is typical Windows behavior, where the Ctrl+Z symbol must be at the beginning of the line or it will not work as expected.

So if you input "foo", then send the EOF signal by pressing Ctrl+Z and then input "bar", "foo" will be read as expected and then EOF will wait in the input buffer until "bar" is typed too. The program, as is, will stop at EOF and "bar" will be ignored, even though the user typed it.

Read more in C++ Issue with cin and CTRL + Z.

while (cin *pchar) awaits further input after Ctrl-Z

If you press Ctrl-Z after typing some other text, it flushes the line buffer (it does not set end-of-file condition).

You have to press it twice in a row; or press it after a newline; to cause the end-of-file condition to occur.

Your misunderstanding is to do with the windows console behaviour, not with C++ streams per se.

See also: Why do I require multiple EOF (CTRL+Z) characters?

Resume reading from iostream::cin after Ctrl+Z (EOF)? (ignore doesn't work)

Hitting Ctrl+z (on Windows) closes the standard input stream. Once it's closed, it stays closed. It doesn't magically reopen once the inner loop is finished. There's just no reason why it would.

cin val sometimes reads 0 depending on Ctrl-Z

Here is what happens. I observed this using MinGW-w64 4.9.2. The behaviour was the same whether running the executable in a Windows console, or under Cygwin (but not using cygwin-mingw).

  • Pressing ^Z at the beginning of a line sets the end-of-file condition
  • Pressing ^Z anywhere else actually sends ASCII 26 character to the stream

I also observed:

  • cin >> val sets val to 0 if it fails due to the input not containing a number.
  • cin >> val leaves val unchanged if input fails due to end-of-file.

According to this thread that is the correct behaviour specified by C++11.

So your results can be explained. When you input 42 42 42 12 13 13^Z, it is the same as if you had written 42 42 42 12 13 13x. The first six numbers are read, and then when x is encoutered, cin >> val fails and sets val to 0.

But when you press Enter and then ^Z, it is as if you were reading from a file and you reached the end of the file. cin >> val leaves val unchanged and it is still holding the value that it had after the last successful cin >> val.

If you make the change suggested by Gautam Jha suggested then you will get 13 in both cases. This is because he effectively reads into a temporary int, and then only stores the temporary int into the real val if the read succeeded, thereby avoiding the behaviour where a failed read sets val to 0.

This is probably the desired behaviour although you might also want to check cnt > 0 to avoid a weird output in the case of totally empty input.



Related Topics



Leave a reply



Submit