1

I'm writing a CLI application in Windows that accepts a string as an input. I assume the end of the input is when user presses Ctrl+Z (that imitates EOF).

When I enter "qwe" and press Ctrl+Z the getline instead of just assigning the "qwe" to tmp asks me to input one more line for some reason. However, the resulting value in tmp is still "qwe" ignoring the extra line. The code I use is the following:

    string tmp;
    getline(cin, tmp);

UPD:

As it was said in C++ Issue with cin and CTRL + Z, it's just the usual Windows behavior, where the Ctrl+Z symbol must be at the beginning of the line.

To get the multiline input you should use read by characters until you meet '\n' || EOF.

Sam
  • 1,832
  • 19
  • 35
  • hmm, looking in the duplicate theme, when I press CTRL+Z in the beginning of my input then it perfectly works. But if it's in the end or in the middle then it asks for an extra line and assigns everything before '^Z' to tmp and only then moves to the following code line. that's if getline is inside of while – Sam Oct 11 '16 at 12:07
  • So is it a question about the behavior of ctrl+z in std::cin or do you simply want to read a file in a string array? Or maybe i understood it wrong? – Treycos Oct 11 '16 at 12:20
  • I need to read stdin into a string array. that's the task. but i assume they are gonna end input with ctrl+z so I want to make sure getline doesn't ask for input after that. or the user just needs to know how to use ctrl+z to? that seems strange. however the task is gonna be checked by codeforces.com i mean by the server, so maybe i just need to leave it as it is – Sam Oct 11 '16 at 13:19
  • Why do you need an array? You need to split the input string? How is the data inputted(if thats a word)? – Treycos Oct 11 '16 at 13:41
  • well, it's just stdin so it should imitate keyboard (maybe just past data, like i do with ctrl+v). I need an array because I need to process every string and then output. The purpose of the program is to format indentations of the text – Sam Oct 11 '16 at 15:00
  • Updated. p.s. Don't look at those comments. – Sam Jun 11 '17 at 10:50
  • 6 months.... Not bad – Treycos Jun 11 '17 at 13:09
  • @Treycos yeah corrected – Sam Jun 11 '17 at 14:13

1 Answers1

1

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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305