我可以做一个循环,直到“std::cin”在输入缓冲区中看到“”字符

Can I make a loop until `std::cin` see the ` ` character in the input buffer?

本文关键字:输入 缓冲区 字符 cin 一个 循环 std 我可以 直到      更新时间:2023-10-16

像这样。

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
    }
}