Custom input stream. Stream buffer and underflow method
<p>To understand how input streams work I designed 2 of the following classes:</p>

<pre><code>#include &lt;iostream&gt;

class my_streambuf: public std::streambuf
{
private:
  std::streambuf* buffer;

  char ch;

protected:

  virtual std::streambuf::int_type underflow()
  {
    std::streambuf::int_type result = buffer-&gt;sbumpc();

    if (result != traits_type::eof())
    {
      ch = traits_type::to_char_type(result);

      setg(&amp;ch, &amp;ch, &amp;ch + 1);
    }

    return result;
  }

public:
  my_streambuf(std::streambuf* buffer) : buffer(buffer) {};

  virtual ~my_streambuf() {};
};

class my_istream: public std::istream
{
public:
  my_istream(std::istream&amp; stream) : std::istream(new my_streambuf(stream.rdbuf())) {};

  virtual ~my_istream()
  {
    delete rdbuf();
  }
};

int main()
{
  char s[32];
  my_istream is(std::cin);

  is &gt;&gt; s;
  std::cout &lt;&lt; s;
  return 0;
}
</code></pre>

<p>Which work fine, until I change the logic of <code>underflow</code> method. The primary goal is to save data in c-string valiable <code>s</code> which differs from user-input. To make a simple test, I changed the <code>underflow</code> method to be the following:</p>

<pre><code>virtual std::streambuf::int_type underflow()
{
  std::streambuf::int_type result = buffer-&gt;sbumpc();

  if (result != traits_type::eof())
  {
    result = traits_type::to_int_type('+'); // &lt;-- this was added

    ch = traits_type::to_char_type(result);

    setg(&amp;ch, &amp;ch, &amp;ch + 1);
  }

  return result;
}
</code></pre>

<p>With the idea being to make the method return only <code>+</code> symbols instead of user-input chars.
So for example if input is <code>123</code>, I expect <code>+++</code> to be stored in variable <code>s</code>.
And that does not work. Console hangs as if it is waiting more input. Only a certain amount of keypressing (or sending EOF) helps.</p>

<p>What am I missing here?</p>

<hr>

<p>As was pointed out by @ferosekhanj, the problem was the missing newline, which was not returned by the modified version of <code>underflow</code> to the caller. So in order for the code to work properly it has to be returned. This version of the method works fine.</p>

<pre><code>virtual std::streambuf::int_type underflow()
{
  std::streambuf::int_type result = buffer-&gt;sbumpc();

  if ((result != traits_type::eof()) &amp;&amp; !traits_type::eq(traits_type::to_char_type(result), '\n'))
  {
     result = traits_type::to_int_type('+');

     ch = traits_type::to_char_type(result);

     setg(&amp;ch, &amp;ch, &amp;ch + 1);
  }

  return result;
}
</code></pre>