Javatpoint Logo
Javatpoint Logo

basic_istream--unget() in C++

The basic_istream::unget() function is used to unget the character, which also decreases the location by a single character and allows the retrieved character to be reused. The appropriate header file should be provided.

The objective of using the basic_istream::unget() method is to return a character to the stream, allowing it to be easily processed at a later stage. This function can lower the location by one character, essentially restoring the previously removed character.

Header File:

It included the <iostream> header file.

Syntax:

It has the following syntax:

Parameters: The basic_istream::unget() function takes no parameters.

Return value: The basic_istream object is returned by the method basic_istream::unget().

The following programs explain std::basic_istream::unget():

Example:

Filename: Ungetting1.cpp

Output:

We got: P
After the ungetting, the character is: P

Example 2:

Filename: Ungetting2.cpp

Output:

We got: L
After the ungetting, the character is: L

Explanation:

  • istringstream con("Language");: It initializes an input string stream con using the string "Language".
  • char a= con.get();: If (con. unget()) is true, it reads the first letter from the stream ('L') and puts it in variable a. { ... }: Moves to "unget" the character from the stream. The unget() method returns the stream location of one character. If successful, it provides true.

Within the if block:

  • char be = con.get();: After unget(), reads an element from the stream. It should return the same element that was previously read ('L') because it was "unget" back to the stream.
  • The values of a and be are printed on the terminal.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA