【问题标题】:std::cin.getline doesn't wait for Inputstd::cin.getline 不等待输入
【发布时间】:2017-09-04 08:27:28
【问题描述】:

here 为例。

相关代码:

    char line[chat_message::max_body_length + 1];

    while (std::cin.getline(line, chat_message::max_body_length + 1))
    {
        chat_message msg;
        msg.body_length(std::strlen(line));
        std::memcpy(msg.body(), line, msg.body_length());
        msg.encode_header();
        c.write(msg);
    }

    c.close();
    t.join();

应用程序立即退出,无需等待我输入聊天消息。 我没有对代码进行任何修改,期望使用 Windows 而不是控制台应用程序(我仍在使用 Alloc&AttachConsole 附加控制台)

【问题讨论】:

    标签: c++ console std cin getline


    【解决方案1】:

    长话短说,您的std::cin istream 无效。 std::cin.getline 返回对 std::istream 的引用,它表示 std::cin。追溯继承层次结构,std::basic_iosstd::cin 派生自,有一个operator bool 重载,如果您的流上的badbiterrorbit 已设置,则将返回false。

    我打赌你的控制台,因为没有以传统方式设置,没有正确连接到std::cin

    【讨论】:

      猜你喜欢
      • 2014-04-07
      • 2011-06-12
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多