The C++ file input and output: using the ignore() and get() member functions code sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the ignore() and get() member functions of the cin in C++ programming

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

 

 

 

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

#include <iostream>

using namespace std;

 

int main (void)

{

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;

return 0;

}

 

Output example:

 

Enter your first and last names: Mike Tyson

The initials letters are: MT

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index