This post will discuss how to read multi-line string input in C++.

The std::getline function extracts characters from the input stream and stores them into a specified stream until a delimiter is encountered. The idea is to read each line at a time with std::getline and store them in a container. If no delimiter is specified, use an empty line as a delimiter character.

Download  Run Code

 
The above solution repeatedly read lines with std::getline using a loop, and store each line as an element in a vector of strings. We can read the entire text in a single string by using the overload version of the std::getline function that accepts a delimiter character to signal the end of the input:

 
For example, the following solution reads the multiline input from the console into the string s using std::getline until the $ character is encountered, which gets interpreted as EOF.

Download  Run Code

 
We can even use the actual EOF (End-of-File) indicator as a delimiter. Now, the input is terminated when the end-of-File indicator associated with the input stream is set.

Download  Run Code

That’s all about reading multi-line string input in C++.