Cpp - Reading a Line

Introduction

The >> operator can only read one word into a string.

To read a line of text, use the function getline().

getline(cin, text); 

This statement reads characters from cin and stores them in the string variable text until a new line character occurs.

You can specify a different delimiting character by passing the character to the getline() function as a third argument.

getline(cin, s, '.'); 

The delimiting character is read, but not stored in the string.

Any characters subsequent to the first period will remain in the input buffer of the stream.

Related Topic