码迷,mamicode.com
首页 > 其他好文 > 详细

关于输入getline

时间:2019-05-26 18:25:36      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:put   inpu   NPU   objects   ade   ase   pre   cin   int   

此函数可读取整行,包括前导和嵌入的空格,并将其存储在字符串对象中。

getline 函数如下所示:

getline(cin, inputLine);

其中 cin 是正在读取的输入流,而 inputLine 是接收输入字符串的 string 变量的名称。下面的程序演示了 getline 函数的应用:

// 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;
}

 

关于输入getline

标签:put   inpu   NPU   objects   ade   ase   pre   cin   int   

原文地址:https://www.cnblogs.com/virtualtan/p/10926601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!