Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: Intermediate
Nov 6, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Reading Strings that Contain White Spaces

The std::getline() functions reads data from an input stream and writes it to a string object. Unlike cin's >> operator, getline() also reads white spaces, which makes it useful for reading strings that contain blanks. For example:

#include <string>
#include <iostream>
int main()
 std::string name;
 cout<<"enter first name and last name: ";
 std::getline(std::cin, name);

getline() has three parameters, the third of which is a delimiter serving as a string terminator. By default, the delimiter's value is '\n'. You can override this value and define a different delimiter. For example, you can define the sign '\t' (tab) as the delimiter:

std::getline(cin, name, '\t');
Danny Kalev
Thanks for your registration, follow us on our social networks to keep up-to-date