Open In App

basic_istream::get() in C++ with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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



Last Updated : 28 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads