The cin.getline() methods to read a line of text from the standard input C++ 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: Reading a line of text from the standard input using cin.getline() method in C++ programming

To show: How to use the C++ cin.getline() method to read a line of text from the standard input using cin.getline() method in C++ programming

 

// getline() example

#include <iostream>

using namespace std;

 

const int SIZE = 100;

 

void main(void)

{

char buffer[SIZE];

 

cout<<"Read by cin.getline(buffer, SIZE)\n"

<<"--------------------------------\n"

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

cin.getline(buffer, SIZE);

cout<<"The line of text entered is: "<<endl;

cout<<buffer<<endl;

}

 

Output example:

 

Read by cin.getline(buffer, SIZE)

--------------------------------

Enter a line of text:

THis is a line of text from standard input

The line of text entered is:

THis is a line of text from standard input

Press any key to continue . . .

 

 

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