Skip to content
Related Articles

Related Articles

basic_istream::get() in C++ with Examples
  • Last Updated : 28 May, 2020

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:




// 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;
}
Output:
First character is: G
After reading:G We got e

Program 2:




// 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;
}
Output:
First character is: C
After reading:C We got o
Now we got : m

Reference: std::basic_istream::get

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :