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 accepts any parameter.
Return Value: The function basic_istream::unget() returns the basic_istream object.
Below is the programs to illustrate std::basic_istream::unget():
Program 1: 
 

CPP14

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for basic_istream::unget()
#include <bits/stdc++.h>
using namespace std;
  
// Driver code
int main()
{
    // Declare string stream
    istringstream gfg("GeeksforGeeks");
  
    char a = gfg.get();
    if (gfg.unget()) {
        char b = gfg.get();
        cout << "We got: " << a << endl;
        cout << "After ungetting the "
             << "character once again"
             << " we got: "
             << b << endl;
    }
  
    return 0;
}

chevron_right


Output:

We got: G
After ungetting the character once again we got: G

Program 2: 
 



CPP14

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for basic_istream::unget()
#include <bits/stdc++.h>
using namespace std;
  
// Driver code
int main()
{
    // Declare string stream
    istringstream gfg("Laptop");
  
    char a = gfg.get();
    if (gfg.unget()) {
        char b = gfg.get();
        cout << "We got: " << a << endl;
        cout << "After ungetting the "
             << "character once again"
             << " we got: "
             << b << endl;
    }
  
    return 0;
}

chevron_right


Output:

We got: L
After ungetting the character once again we got: L

Reference: http://www.cplusplus.com/reference/istream/basic_istream/unget/

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.