This post will discuss how to read a string from standard input in C++.

1. Using std::getline

A simple solution is to use the std::getline function, which extracts characters from the input stream and stores them into the specified string until a delimiter is found. Here’s what the code would look like:

Download  Run Code

 
We can extend the solution to continuously read lines until an empty line is encountered.

Download  Run Code

2. Using std::cin

Alternatively, we can use std::cin to read from the standard input. The std::cin represents the standard input stream, in the same way std::cout represents the standard output stream.

Download  Run Code

That’s all about reading a string from standard input in C++.