标题c++ 中的getLine 函数
类似于 Java 中 string nextLine()函数,获取当前输入的一行。
// 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**
// getline()属于string流
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;
}
可以把空格,或回车 ,同时一起获取 。
本文介绍C++中的getline函数,类似于Java中的stringnextLine,用于读取一行字符数据到字符串对象。通过示例代码展示了如何使用getline函数获取带有空格和回车的整行输入。
301

被折叠的 条评论
为什么被折叠?



