Cpp Basic Programming 9

getline()
cin.getline():
This function helps us to take input for group of characters including spaces of specified length or till enter is pressed.
Header file: iostream.h

Syntax:
cin.getline(n,s);
n: variable(character array)
s: size

it will take input for maximum s-1 characters. One location is reserved for NULL(‘\0’) character.

Example:
cin.getline(n,20);
It will take input for maximum 19 characters. One location is reserved for NULL (‘\0’) character.

cin.getline(n,5);
It will take input for maximum 4 characters. One location is reserved for NULL (‘\0’) character.

If we enter “hello”
It will store only “hell”

#include<iostream>
using namespace std;
int main()
{
	char n[20];
	cout<<"Enter any string ";
	cin.getline(n,20);
	cout<<"String is "<<n<<endl;
	return(0);
}
/* Output */
Enter any string hello world
String is hello world
#include<iostream>
using namespace std;
int main()
{
	char n[20];
	cout<<"Enter any string ";
	cin.getline(n,5);
	cout<<"String is "<<n<<endl;
	return(0);
}
/* Output */
Enter any string hello
String is hell