About getline input

This function reads the entire line, including the leading and embedded spaces, and stored in a String object.

getline function as follows:

getline(cin, inputLine);

Where cin is the input stream is being read, the string variable name inputLine received input string. The following program demonstrates the use of the getline function:

// This program illustrates using the getline function
//to read character data into a string object.
#include <iostream>
#include <string> // Header file needed to use string objects
using namespace std;
int main()
{
string name;
string city;
cout << "Please enter your name: ";
getline(cin, name);
cout << "Enter the city you live in: ";
getline(cin, city);
cout << "Hello, " << name << endl;
cout << "You live in " << city << endl;
return 0;
}

 

Guess you like

Origin www.cnblogs.com/virtualtan/p/10926601.html