The getline() function has the following two syntaxes:
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
s: Pointer to an array of characters where extracted characters are stored as a c-string.
n: Maximum number of characters to write to s (including the terminating null character).
delim: Explicit delimiting character
The return type of this function is istream object (*this)
.
In the above scenario, the data is read into the pointer to an array of character, test, which is converted at runtime and hence can store up to 50 characters as declared in the cin.getline(test, 50)
.
If you want to achieve your desired result kindly use n=10
cin.getline
even exists. Usestd::getline
instead. It isn't perfect, but for most situations, it's a definite improvement. – Fahy