【发布时间】:2012-03-13 14:06:06
【问题描述】:
我有一个计时器线程,它会在未来执行 5 秒,并有一个循环等待它完成执行。然后程序在用户按回车时结束。我注意到,在等待循环时,输入缓冲区被接受,并用于完成程序,任何后续的输入命中都被输入到命令行!
我想忽略在“按回车键退出”之前输入的所有输入,包括回车。自从我使用 C++ 以来已经有一段时间了,我不记得如何做到这一点(我已经搜索过 SO 和 Google,但找不到这个特定问题的答案)。这是我的例子:
std::cout << "Timer test: wait 5 seconds\n";
boost::asio::io_service test_io;
deadline_timer test_timer(test_io, posix_time::seconds(5));
int testInt = 0;
auto asynctest = [&testInt](const boost::system::error_code&) {
std::cout << "Running asynctest()\n";
testInt = 5;
};
std::cout << "Starting asynchtest, which should output in 5 seconds\n";
test_timer.async_wait(boost::bind<void>(asynctest, boost::asio::placeholders::error));
while(testInt != 5) {
std::cout << ". ";
boost::this_thread::sleep(boost::posix_time::milliseconds(200));
}
// How do I clear all input from the input stream here so that if the user hit enter
// during the timer countdown it will be cleared and user still must hit enter to
// exit program?
std::cout << "Press enter to exit\n";
std::cin.ignore(80, '\n');
return 1;
广告
【问题讨论】:
标签: c++ boost input timer flush