Namespaces
Variants
Actions

Talk:cpp/io/basic istream/putback

From cppreference.com
< Talk:cpp‎ | io

I have implemented this program to understand more `cin.putback();`:


   std::string str;
   while( std::cin.peek() != EOF){
       if( std::cin.get() == '*')
           std::cin.putback('$');
       else
           std::cin.unget();
       str += std::cin.get();
   }

Input:

   Hello***There!

output:

   Hello$$$there!
  • The program looks to work fine but in my case I extract '*' as the last character read but calling std::cin.putback('$') in which case they are not identical characters. And in the text above it is said that "...takes an argument that must be the same as the one that was last read."

So could you please clarify it? — Preceding unsigned comment added by Raindrop7 (talkcontribs)

The text you are quoting does not appear in our page, so there's nothing to clarify. T. Canens (talk) 18:17, 12 February 2021 (PST)