0
Question
I am wondering how do I differentiate between gets() function and getline() . Anyone please help me out?
3 Answers
+ 13
gets() is to input a character array whereas cin.getline() is to input objects of standard string class. So, if you are working on a character array, use gets(). And if you are using string object, use cin.getline().
https://stackoverflow.com/questions/26873401/difference-between-cin-get-and-cin-getline
+ 6
The cin.getline() method is used to read a max of n chars to a character array. Eg :
char arr[1000];
cin.getline(arr,100);
Now getline stops at the moment 100 characters are complete or it receives a delimiter which is newline by default. Also, if the size is greater than the array size, it will set failbit and stops.
The gets() function is a C function deprecated since C++11, and should no longer be used. It reads characters for an array till it encounters \n buy has no limit to stop and so can even alter memory locations it should not.
Read more here :
www.gidnetwork.com/b-56.html
www.cplusplus.com/reference/cstdio/gets
www.cplusplus.com/reference/istream/istream/getline
+ 3
@SD
Only the friend version of getline() works on std::string. The method version of getline() is used for character arrays or pointer arrays and can read a fixed number of characters or less when it receives an optional delimiter which is \n by default.
Eg :
// friend version.
std::string str;
getline(cin,str);
// method version.
char arr[1000];
cin.getline(arr,100);
//Reads 100 chars at max, even
//when arr can hold 1000.
Secondly, you posted the link for differences between get and getline, when @Myo Thura Htwe required the difference between gets() and getline(), and gets() and get() are different functions.