1

Hi guys i have the following question:

string s;

std::cout << "give me a string" << std::endl;  
while (std::cin >> s){
    std::cout << "give me a string" << std::endl;
}
cin.clear();  

if s was an integer then i know that if i typed a char then the while would break. If it was an integer and typed a double the same would happened. But what about strings ?? Somewhere I read that in windows (as I am writting at netbeans in windows) you have to press ctrl+z and then it stops as it is taken as a false string. But when i do this , the process freezes and nothing happens.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Kostas Ayfantop
  • 53
  • 1
  • 1
  • 10

2 Answers2

2

You need to send the EOF signal, which in Netbeans is Ctrl + D.

However, hitting Enter twice will result in the condition of your loop to resolve in false, causing the loop to stop. That's excpected: How is "std::cin>>value" evaluated in a while loop?

If that doesn't work, use Ctrl + z and a newline character, which is the EOF signal in Windows, as stated here. Ctrl + D is for Linux though, but it works in your case.

The EOF signal closes the input stream, thus breaking the loop.


Moreover, in Netbeans (if the above doesn't work) it seems that you need to run your application from the command prompt to achieve this. Another attempt would be to "press Ctrl + z after pressing Enter", as stated here.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    Not on Linux. Pressing `Ctrl + D` there does finish the stream and hence the program, though. – jdehesa Sep 08 '17 at 11:10
  • I tried double enter on VS 2012 and it does not work (tested in default console). Precisely, pressing enter does not enter into a loop brackets. – R2RT Sep 08 '17 at 11:10
  • thanks for the answer , actually it won't stop , as it changes 2 lines and after that it waits for an input. If I type something it will accept it. @jdehesa is so right , it actually worked!! thx – Kostas Ayfantop Sep 08 '17 at 11:12
  • @gsamaras Yes, I mean, I can't really say for sure why two enters do not work on Linux (I guess it does on Windows? I haven't checked), or whether it should, I just tested the program and noted it didn't. Maybe it's a Windows-specific thing? – jdehesa Sep 08 '17 at 11:24
1

The loop will stop when the input stream is closed.

If you run the program from a shell, then the input stream may be open indefinitely. Exactly how you would close the stream in a terminal, depends on what terminal you are using.

In UNIX, the stream is closed when you send empty input. ctrl + d is the standard key combination for sending (flushing) the input to the stream (^d is the ASCII control code EOT, end of transmission). Pressing enter also sends the input, but it also inserts the end of line character, so it is not possible to use it to send empty input.

ctrl + z is similar in windows, but it has different behaviour (wikipedia):

The EOT character in Unix is different from the Control-Z in DOS. The DOS Control-Z byte is actually sent and/or placed in files to indicate where the text ends. In contrast the Control-D causes the Unix terminal driver to signal the EOF condition, which is not a character, while the byte has no special meaning if actually read or written from a file or terminal.

eerorika
  • 232,697
  • 12
  • 197
  • 326