首页 > 解决方案 > 为什么 fullName 在空格后不显示名称

问题描述

最后的 cout 没有显示 cin fullName

int main()
{
    string fullName;
    cout << "Type your full name: ";
    cin >> fullName;
    cout << "Your name is: " << fullName; //this final cout is not displaying the cin fullName
    system("pause>0");
}

标签: c++string

解决方案


cin不适用于输入中的空格,输入的其余部分在缓冲区中。尝试getline像这样使用:

 int main()
{
    string fullName;
    cout << "Type your full name:" ;
    getline(cin,fullName);
    cout << "Your name is: " << fullName; 
    system("pause>0");
}

这样,它会将所有内容保存到您的变量中,直到达到\n.


推荐阅读