-1

(New to C++)

I have this simple code (simplified for the question):

 int main()
{
    string currInput;
    while (getline(cin, currInput))
    {
    }
    cout << "wont be printed" << std::flush;
    return 0;
}

I have been debugging for a while and I probably miss something:

When running it and pressing ctrl+d (after some strings or right away), it does not print the string that is after the while loop. It just stop running. I thought it might be something with flushing so I added that too. What am I missing?

PS: When running in debug, it mentions something about sighup signal.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Kobi T
  • 417
  • 6
  • 12

2 Answers2

1

So, with a lot of help from @manni and @rici, we found the problem.

It turns out to be a known problem in cLion.

see sending EOF to stdin in Clion IDE

https://intellij-support.jetbrains.com/hc/en-us/community/posts/206849765-How-to-enter-EOF-Ctrl-z-in-Run-console-

https://youtrack.jetbrains.com/issue/IDEA-12514

Kobi T
  • 417
  • 6
  • 12
-2

Your error is the while loop. You should not be using a loop here. What happens is it hits this line and attempts to get input. Regardless of whether you type in anything or not, if you hit CTRL+D it will end the getline. But you have it in a while loop....so it'll hop back to the top of the loop and get another line...then another line....then another line. Welcome to your first infinite loop.

while (getline(cin, currInput))
{
}

The simplest thing would be to do just

getline(cin, currInput);

If you're starting out programming this is probably what you want to do anyway.

If you feel gutsy, read this page: http://www.cplusplus.com/reference/string/string/getline/

You'll notice that getline returns the stream you pass in. Which will evaluate to true as far the loop is concerned.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
Jerry Saravia
  • 3,737
  • 3
  • 24
  • 39
  • First of all, thanks for your comment. I know that's not the right way to write this code, I wanted to simplify a more complex code for the question. It doesn't enter an infinite loop, it exits and return 0 without printing the last line. – Kobi T Aug 29 '17 at 21:35
  • oh, lol. You said "(New to C++)" so I imagined you were brand spanking new. – Jerry Saravia Aug 29 '17 at 21:37
  • @KobiT If that's the case, then the SIGHUP link is a good way to go. Understand how to handle the signal. – Jerry Saravia Aug 29 '17 at 21:39
  • how does this code work without handling it: https://stackoverflow.com/questions/20756968/reading-multiple-lines-from-a-file-using-getline In which cases should I handle it? I've seen many examples of while loop with getline and none of them mentioned it. – Kobi T Aug 29 '17 at 21:41
  • http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool explains how functions which return an `istream` are *designed* to work with conditionals and `while` statements. – rici Aug 29 '17 at 21:43
  • Thanks, @rici. My interpretation of the istream return from the `getline` call is wrong. It should be obvious by now I don't do C++ often. – Jerry Saravia Aug 29 '17 at 21:51
  • Jerry, since OP's code actually works (in a terminal), you might want to consider editting your answer. – rici Aug 29 '17 at 22:11