+ 2

What is getline() ?

Pls clearly explain what is getline() and how to use it?

11th May 2018, 5:19 PM
Shahil Ahmed
Shahil Ahmed - avatar
9 Réponses
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.
11th May 2018, 5:34 PM
MsJ
MsJ - avatar
+ 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
11th May 2018, 6:04 PM
NezhnyjVampir
+ 7
https://www.sololearn.com/Discuss/941461/?ref=app
11th May 2018, 5:31 PM
NezhnyjVampir
+ 3
a function to get input getline(cin, variableName);
11th May 2018, 5:29 PM
Kawaii
Kawaii - avatar
+ 3
NezHynjVampir currently in cpp the string library is included in iostream so i think i don't have a need to include that??
11th May 2018, 5:35 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 3
can anyone give me a example code so i can understand better
11th May 2018, 5:36 PM
Shahil Ahmed
Shahil Ahmed - avatar
+ 2
if you remove #include <string> from those codes it still works perfectly
12th May 2018, 4:20 AM
Shahil Ahmed
Shahil Ahmed - avatar
+ 2
Thanks guys i got what is getline() https://code.sololearn.com/c3Ntg3J33z7g/?ref=app
12th May 2018, 4:32 AM
Shahil Ahmed
Shahil Ahmed - avatar
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;}
11th May 2018, 5:42 PM
MsJ
MsJ - avatar