接受字符串中的空字符空间

时间:2015-11-23 00:00:39

标签: c++

我正在尝试让字符串接受字符空间。但是当你输入类似的内容时:

Mike Smith

输出只是

麦克

有没有一种简单的方法可以解决这个问题? 我可以这样做:

string playerName(50)< - 其中(50)是字符串的长度

我的代码如下:

int main() {
string playerName;
cout << "Enter Player Name: ";
    cin >> playerName;

cout << playerName << endl;
}

2 个答案:

答案 0 :(得分:2)

使用以下内容阅读整行输入:

getline(cin, playerName);

答案 1 :(得分:1)

使用:

getline(cin, playerName);

但请记住,在使用上述行之后,在获取任何其他数据类型之前必须使用以下行。

cin.ignore();
相关问题