像这样的东西..
do{
int x{};
std::cin >> x;
}while(/*i want the condition mentioned here*/);
我的意思是,如果用户输入x
并按Enter,我希望循环结束。我可以这样做吗?
最常用的方法是读取整行,然后从该行中提取数据。
也就是说,视角从“阅读内容直到遇到换行符”转变为稍微更高级别的“阅读所有内容”。
std::string line;
if (std::getline(std::cin, line))
{
std::stringstream ls(line);
int x = 0;
while (ls >> x)
{
// Process x
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句