Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我在获取字符串时遇到问题。我用
getline(cin,string);
但是有某种错误,当我按下回车键时它会跳过一行,有没有解决这个问题的方法,或者可能是另一个函数来获取带有空格的字符串?
我的猜测是你在cin >> someVar做getline().
cin >> someVar
getline()
cin >> someVar 不读取完整的行,但在第一个空白字符处停止,并且换行符\n保持未使用。,这会导致在getline()
\n
如果是这种情况,
要修复它,您需要在cin.ignore()之前添加一条语句getline()以使用流提取器\n留在输入流中的换行符(或任何其他额外字符) 。>>
cin.ignore()
>>