The effect of in_avail() is entirely implementation-defined. In particular, In many C++ implementations (likely including yours), std::cin has no buffer by default, so in_avail() always returns zero.
If you're using gcc, add the line
somewhere near the beginning of main. That turns on cin's buffer in GNU library (but, for example, in LLVM library, you'd still have zero buffer)
Last edited on
Thanks so much Cubbi. I added the line
cin.sync_with_stdio(false); and it worked. The only problem is that it returns one more than the actual no. of additional characters.
Although I can manage it by manually subtracting 1 from returned value, I was curious why would the function do so.