Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap