basic_istream::unget() in C++ with Examples
Last Updated :
28 May, 2020
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
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;
}
|
Output:
We got: G
After ungetting the character once again we got: G
Program 2:
CPP14
#include <bits/stdc++.h>
using namespace std;
int main()
{
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;
}
|
Output:
We got: L
After ungetting the character once again we got: L
Reference: http://www.cplusplus.com/reference/istream/basic_istream/unget/
Share your thoughts in the comments
Please Login to comment...