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 method basic_istream::get() returns a character if available, otherwise returns end of the file.



Below are the programs to illustrate basic_istream::get()

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for basic_istream::get()
#include <bits/stdc++.h>
using namespace std;
  
// Driver Code
int main()
{
    // Declare string stream
    istringstream gfg("GeeksforGeeks");
  
    // Here we get H
    char a = gfg.get();
    cout << "First character is: "
         << a << endl;
  
    char b;
    // Here we got e
    gfg.get(b);
    cout << "After reading:" << a
         << " We got " << b << endl;
  
    return 0;
}

chevron_right


Output:

First character is: G
After reading:G We got e

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for basic_istream::get()
#include <bits/stdc++.h>
using namespace std;
  
// Driver Code
int main()
{
    // Declare string stream
    istringstream gfg("Computer");
  
    // Here we get C
    char a = gfg.get();
    cout << "First character is: "
         << a << endl;
  
    char b;
    // Here we got o
    gfg.get(b);
    cout << "After reading:" << a
         << " We got " << b << endl;
  
    char c;
    // Here we got m
    gfg.get(c);
    cout << "Now we got : "
         << c << endl;
  
    return 0;
}

chevron_right


Output:

First character is: C
After reading:C We got o
Now we got : m

Reference: std::basic_istream::get

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




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.