Tuesday, 22 September 2015

How to use the cin get() and ignore() member functions to extract and discard a stream in C++ programming

Leave a Comment
// the istream cin. ignore() and cin.get() member functions

#include <iostream>
using namespace std;
void main ()
{
       char firstword, secondword;

       cout<<"Enter your first and last names: ";

       // read and store in firstword, get() function includes white-space characters

       firstword = cin.get();

       // extracts up to 30 elements and discards them, a space, if encountered before 30,
       // causes ignore() to return and

       cin.ignore(30,' ');

       // allowing all elements after a space to be read.
       secondword = cin.get();
       cout<<"The initials letters are: "<<firstword<<secondword<<endl;
}



Output


If You Enjoyed This, Take 5 Seconds To Share It