+ 2
What is getline() ?
Pls clearly explain what is getline() and how to use it?
9 Answers
0
getline() function is used when you have to take input strings which have spaces in between them. using cin you cannot take input the entire string have words in between them. getline() is a standard library function in C++ and is used to read a string or a line from input stream. It is present in the <string> header.
+ 9
Shahil Ahmed, as long as I remember, we have to include each library explicitly...
You can find many examples using the search bar in the Code Playground section:
https://code.sololearn.com/cy2IYHzeYXyu/?ref=app
https://code.sololearn.com/cp32oQ9205NP/?ref=app
https://code.sololearn.com/chdC5m1X4boX/?ref=app
+ 7
https://www.sololearn.com/Discuss/941461/?ref=app
+ 3
a function to get input
getline(cin, variableName);
+ 3
NezHynjVampir currently in cpp the string library is included in iostream so i think i don't have a need to include that??
+ 3
can anyone give me a example code so i can understand better
+ 2
if you remove #include <string>
from those codes it still works perfectly
+ 2
Thanks guys i got what is getline()
https://code.sololearn.com/c3Ntg3J33z7g/?ref=app
0
Here is an example Code :
#include<iostream>
using namespace std;
int main(){Â
/*When you are Dealing with array of characters as a String*/
char String1[1000];
cout<<"Enter the String"<<endl<<endl;
cin.getline(String1,1000);
cout<<"You entered :"<<endl;
cout<<String1;
cout<<endl;Â
/*When you are dealing with string using stdc++ liberary*/
string string2;
cout<<"Enter the String "<<endl<<endl;
getline(cin,string2);
cout<<"You enterd : "<<endl;
cout<<string2;
cout<<endl;
return 0;}