Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
basic_istream::putback() in C++ with Examples
Next article icon

basic_istream::putback() in C++ with Examples

Last Updated : 28 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
The basic_istream::putback() used to put the character back in the input string. This function is present in the iostream header file. Below is the syntax for the same: Header File:
  #include<iostream>  
Syntax:
  basic_istream& putback (char_type ch);  
Parameter:
  • ch: It represents the character to be put into the input string back.
Return Value: The iostream::basic_istream::putback() return the basic_istream object. Below are the programs to understand the implementation of std::basic_istream::putback() in a better way: Program 1: CPP14
// C++ code for basic_istream::putback() #include <bits/stdc++.h>  using namespace std;  int main() {     stringstream gfg1("GeeksforGeeks");     gfg1.get();      // putback A into the input string     if (gfg1.putback('A'))         cout << gfg1.rdbuf() << endl;      istringstream gfg2("GeeksforGeeks");     gfg2.get();      if (gfg2.putback('A'))         cout << gfg2.rdbuf() << endl;     else         cout << "putback is failed here\n";      gfg2.clear();      // Again putback G in the string     if (gfg2.putback('G'))         cout << gfg2.rdbuf() << endl; } 
Output:
  AeeksforGeeks  putback is failed here  GeeksforGeeks  
Program 2: CPP14
// C++ code for basic_istream::putback() #include <bits/stdc++.h>  using namespace std;  int main() {     stringstream gfg1("GOOD");     gfg1.get();      // putback B into the input string     if (gfg1.putback('B'))         cout << gfg1.rdbuf() << endl;      istringstream gfg2("GOOD");     gfg2.get();      if (gfg2.putback('B'))         cout << gfg2.rdbuf() << endl;     else         cout << "putback is failed here\n";      gfg2.clear();      // Again putback G in the string     if (gfg2.putback('G'))         cout << gfg2.rdbuf() << endl; } 
Output:
  BOOD  putback is failed here  GOOD  
Reference: http://www.cplusplus.com/reference/istream/istream/putback/

Next Article
basic_istream::putback() in C++ with Examples

B

bansal_rtk_
Improve
Article Tags :
  • Misc
  • C++
  • CPP-Functions
Practice Tags :
  • CPP
  • Misc

Similar Reads

    basic_istream::peek() in C++ with Examples
    The std::basic_istream::peek() used to reads the next character from the input stream without extracting it. This function does not accept any parameter, simply returns the next character in the input string. Below is the syntax for the same: Header File: #include<iostream> Syntax: int peek();
    2 min read
    basic_istream::unget() in C++ with Examples
    The basic_istream::unget() is used to unget the character and used to decrease the location by one character and makes the extracted character available for used once again. Header File: <iostream> Syntax: basic_istream& unget(); Parameters: The basic_istream::unget() method doesn’t accept
    2 min read
    basic_istream::swap() in C++ with Examples
    The basic_ios::swap(x) is used swap all data members of the base class except for rdbuf(), and swaps the values of the gcount() counters between *this and x. This basic_ios::swap(x) function is a protected function. Below is the syntax and the header file for the same: Header File: #include<iostr
    2 min read
    basic_istream::seekg() in C++ with Examples
    The basic_stream::seekg() method is used to set position of the next character to be extracted from the input stream. This function is present in the iostream header file. Below is the syntax for the same: Header File: #include<iostream> Syntax: basic_istream& seekg (pos_type pos); Paramet
    2 min read
    basic_istream::get() in C++ with Examples
    The basic_istream::get() is used to get the character. This function returns a one character if available, otherwise it will return end of the file. Header File: <iostream> Syntax: int_type get(); Parameters: The method basic_istream::get() doesn’t accept any parameter. Return Value: The metho
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences