The C++ getline() member function program example

 

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 C++ getline() member function to extract strings from the input stream line by line in C++ programming

To show: How to use the C++ getline() member function to extract strings from the input stream line by line in C++ programming

 

// the C++ getline() program example

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

string str;

string str1;

string str2;

 

cout<<"Enter a line of text: ";

getline(cin, str);

cout<<"You entered: "<<str<<endl;

cout<<"Enter a line of text, <space> as the delimiter: "<<endl;

getline(cin, str1, ' ');

cout<<"You entered: "<<str1<<endl;

 

return 0;

}

 

Output example:

 

Enter a line of text: This is a test line of text

You entered: This is a test line of text

Enter a line of text, <space> as the delimiter:

This is another line of text

You entered: This

Press any key to continue . . .

 

 

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