【发布时间】:2014-08-05 23:15:57
【问题描述】:
所以,我刚开始使用 C++,但遇到了问题。我声明了一个字符串变量等等,但是当我输入给变量一个值,然后尝试显示它时,它只显示句子的第一个单词。
#include <iostream>
#include <string>
using namespace std;
using std::string;
int main()
{
string answer;
cout << "Give me a sentence and I will repeat it!";
cin >> answer;
cout << answer;
return 0;
}
例如,我输入“Yay it working!”,它输出“Yay”
【问题讨论】:
-
cin读取输入直到遇到空白。使用std::getline。 -
cin >> answer;请改用std::getline(cin,answer)。了解您的手册!