Let us say, we want to read a char (formatted input) and a string (using unformatted input) from stdin. We write:
1 2 3 4
char choice ;
std::string name ;
std::cin >> choice ;
std::getline( std::cin, name ) ;
We run the program and enter A<newline> on stdin. std::cin >> choice ; reads 'A' into choice and leaves the <newline> in the input buffer.
The program continues with std::getline( std::cin, name) ;
The getline() sees that the input buffer is not empty, so it does not wait for any input to be entered. It returns immediately after reading an empty string, and extracting the <newline> in the buffer and throwing it away.
We can make the stream discard trailing white space left by the formatted input operation by:
1 2 3 4 5
char choice ;
std::string name ;
std::cin >> choice ;
std::cin.ignore( 10000, '\n' ) ;
std::getline( std::cin, name ) ;
std::cin.ignore( 10000, '\n' ) ; will extract and discard up to 10000 characters or until a '\n' is extracted and thrown away. The 10000 in the example is just a reasonably large enough value, because we don't expect that more than that many characters would need to be discarded.