cin get() in C++ with Examples

cin.get() is used for accessing character array. It includes white space characters. Generally, cin with an extraction operator (>>) terminates when whitespace is found. However, cin.get() reads a string with the whitespace.

Syntax:

cin.get(string_name, size);

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate cin.get()
  
#include <iostream>
using namespace std;
  
int main()
{
    char name[25];
    cin.get(name, 25);
    cout << name;
  
    return 0;
}

chevron_right


Input:

Geeks for Geeks

Output:



Geeks for Geeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to demonstrate cin.get()
  
#include <iostream>
using namespace std;
  
int main()
{
    char name[100];
    cin.get(name, 3);
    cout << name;
  
    return 0;
}

chevron_right


Input:

GFG

Output:

GF

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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 :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.