C++ Program#202 – To workout with cin.getline() and cout.write().

C++ Console Input/Output streams cin and cout provides extension through cin.getline() and cout.write() functions.

cin.getline() : extracts characters from input stream and stores them into string including space.

Syntax :
(1) istream& getline (char* s, streamsize n );

Read(extracts) and store characters from input stream until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character). The delimiting character is the newline character (‘\n’) by-default.

(2) istream& getline (char* s, streamsize n, char delim );

Read(extracts) and store characters from input stream until delim character found in the input sequence, or n character has been read.

cout.write() : sends(inserts) characters to output stream to display over the console.

Syntax :
cout.write(char *s,streamsize n);

Display only specified number of character n from string s.

It is also possible to concatenate two strings while displaying using write such as –

cout.write(“Hello”,5).write(” & Welcome”,9);

instead of writing in two lines.

cout.write(” & Welcome”,9);

Here is an example explaining the topic.

Output : please scroll down.