UPDATE 1: (Thanks to jlb for the help ..)
The code works fine in Linux. (I tested it on Ubuntu, gcc version 4.6.3)
But shows the above behaviour on MAC OSX, gcc version 4.2.1 ... Hope some one can help me solve this ..
UPDATE 2: Solution!! (Thanks to Cubbi ... )
adding the following after line 13 to clear the error bits of the stdin stream solves the problem. clearerr(stdin);
Note:
I need the user to Enter EOF (to detect end of input).
Because I intend to read and store a block of text (spanning multiple lines) from the user like so:
Just in case .. I tried the same code again( I'm using MAC OSX, G++ compiler) but it behaves as I mentioned earlier .. it's not waiting at the getline on [line 16]
Could you tell me what OS you are on and the compiler you used .. though i'm not sure it depends on the OS..
once the input is closed, there is no portable way to reopen it. Imagine how this would work if the program is running with input redirection, reading from a file.
that said, try clearerr(stdin); in addition to your cin.clear(); -- C streams have their own error bits.
Thanks for the reply, but I don't think unwanted characters are causing the issue here. The stream is already empty, I am only passing EOF as the input at the first getline() call on line 11, and using getline() function removes the newline character from the stream too .. (although after EOF I dont think the newline matters)
PS: Cubbi's suggestion solved my problem. You might want to look at it. :)