In some cases, it’s desirable to input an entire line of text into a built-in array of char
s. For this purpose, the cin
object provides the member function getline, which takes three arguments—a built-in array of chars in which the line of text will be stored, a length and a delimiter character. For example, the statements
char sentence[ 80 ];
cin.getline( sentence, 80, '
' );
declare sentence
as a built-in array of 80 characters and read a line of text from the keyboard into the built-in array. The function stops reading characters when the delimiter character '
'
is encountered, when the end-of-file indicator is entered or when the number of characters read so far is one less than the length specified in the second argument. The last character in the built-in array is reserved for the terminating null character. If the delimiter character is encountered, it’s read and discarded. The third argument to cin.getline
has '
'
as a default value, so the preceding function call could have been written as:
cin.getline( sentence, 80 );
Chapter 13, Stream Input/Output: A Deeper Look, provides a detailed discussion of cin.getline
and other input/output functions.
3.141.4.179