c++ - 为什么 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");
}
解决方案
cin
不适用于输入中的空格,输入的其余部分在缓冲区中。尝试getline
像这样使用:
int main()
{
string fullName;
cout << "Type your full name:" ;
getline(cin,fullName);
cout << "Your name is: " << fullName;
system("pause>0");
}
这样,它会将所有内容保存到您的变量中,直到达到\n
.
推荐阅读
- bash - grep:文件中的行没有这样的文件或目录
- android - FireBase 按文档和集合进行多个查询
- node.js - 如何离开带有 vue-socket 的套接字房间并重新加入而不重复消息?
- c++ - 使用自定义类型定义多个同名函数
- azure - ADF 活动未返回正确的错误代码
- c++ - 如何在地图中使用位集?
- julia - 以有效的方式从字符串值转换为浮点值与缺失值的 DataFrame 列
- powershell - 如何在 PowerShell 中获取 Windows.System.Profile.SystemIdentification.Id?
- mongodb - 查询以从 mongodb 对象的嵌套数组中获取 id
- python - Tkinter Entry() 函数不起作用?输出只有一个输入框,而不是 3。有什么问题?