C++ 如何在C+中故意跳过cin+;?
我需要运行一个while循环,并在有输入时接受输入。我不是C++新手,但这个障碍很难。由于保密协议(这个学校项目显然是一些秘密的东西),我只能给你看测试用例 我一直在寻找解决问题的救命稻草;试试catch,cin.get,cin.peek,如果(cin.peek){}。如果有人能给我指出正确的方向,我将非常感激 程序不是时间关键型的,但需要以固定的时间间隔调用函数。代码的可移植性、组合性或诸如此类的东西并不重要;该代码只能在至少具有双核处理器的Windows 7或Windows 8 PC上运行C++ 如何在C+中故意跳过cin+;?,c++,cin,C++,Cin,我需要运行一个while循环,并在有输入时接受输入。我不是C++新手,但这个障碍很难。由于保密协议(这个学校项目显然是一些秘密的东西),我只能给你看测试用例 我一直在寻找解决问题的救命稻草;试试catch,cin.get,cin.peek,如果(cin.peek){}。如果有人能给我指出正确的方向,我将非常感激 程序不是时间关键型的,但需要以固定的时间间隔调用函数。代码的可移植性、组合性或诸如此类的东西并不重要;该代码只能在至少具有双核处理器的Windows 7或Windows 8 PC上运行
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int input = 0;
int pastTime, nowTime;
pastTime = nowTime = time(0);
cin >> input;
while(input != -1)
{
if(input == 1)
{
cout << "Entered 1" << endl;
//To be done instead of the two 'elses',
//bypassing interval-dependant code
}
else if(input == 2)
{
cout << "Entered 2" << endl;
//To be done instead of the interval-dependant code
}
else if(pastTime == (nowTime - 5))
{
cout << "Nothing entered." << endl;
//Needs to be done with a fixed interval.
}
nowTime = time(0);
cin >> input;
}
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int输入=0;
过去的时间,现在的时间;
过去时间=现在时间=时间(0);
cin>>输入;
while(输入!=-1)
{
如果(输入=1)
{
cout我会将读取输入与cin
完全分离,并执行默认超时功能。您需要类似于后台线程的东西,根据时间间隔执行默认功能。要处理前两种情况,您需要向线程发出跳过下一次执行的信号(如果确实需要),只需调用您想要的任何函数或不执行任何操作。简单的答案是将以某个间隔运行的代码放在另一个线程上。因为您已经注意到这是Windows,您可以使用:
从常规开始,开始和停止与时间相关的工作:
HANDLE Start(HANDLE hTimerQueue)
{
DWORD timerMS = 5000; /* every 5 seconds */
HANDLE hTimer;
if (!CreateTimerQueueTimer(&hTimer,
hTimerQueue,
(WAITORTIMERCALLBACK)timerWork,
/*lpParam*/NULL,
/*start in ___ ms:*/0,
/*run every __ ms:*/timerMS,
/*flags*/0))
{
return NULL;
}
return hTimer;
}
BOOLEAN Stop(HANDLE hTimerQueue, HANDLE hTimer)
{
if (!DeleteTimerQueueTimer(hTimerQueue,
hTimer,
/*wait for our timer to complete*/INVALID_HANDLE_VALUE))
{
return FALSE;
}
return TRUE;
}
然后将与时间相关的工作放入自己的回调中:
VOID CALLBACK timerWork(PVOID lpParam, BOOLEAN TimerOrWaitFired /*ignored*/)
{
for (int ii = 0; ii < 10; ++ii) {
std::cout << "timer work: " << ii << std::endl;
Sleep(250);
}
}
VOID CALLBACK timerWork(PVOID lpParam,BOOLEAN TimerOrWaitFired/*忽略*)
{
对于(int ii=0;ii<10;++ii){
std::cout你是说你想在等待终端输入时超时吗?有一些关于对std::cin@的非阻塞调用的讨论,但结果是,可能更容易像上面那样保持循环,并创建第二个线程以固定的时间间隔执行工作。@ahenderson如果此代码没有,你将丢失一个撇号和一个e;-)不必是可移植的,您可能会发现有更好的方法使用合适的特定于平台的库来执行非阻塞或异步IO,而不是使用相对较高级别的iostream
抽象。在嵌入式系统中,您经常会遇到类似的问题,通常需要额外的线程来解决。谢谢,我结合James Beilby的优秀链接,我将继续努力。我从来没有使用过多个线程,我的教科书也没有用一个词来提及它-所以,显然,与你们相比,我是一个新手。与我读过的关于多线程的一点内容相比,你的解决方案对我的应用程序来说似乎有点过头了。谢谢你的帮助和回复-如果多线程杀死我,我将使用它;)如果您正在寻找,我可以添加一个简单的Jane线程示例。诀窍是正确停止并恢复与时间相关的代码。谢谢,但我使用Boost:Thread获得了它。它将更新问题以反映问题已解决。@CarstenNielsen:很酷,但无需编辑标题或者你的帖子说“已解决”。你所需要做的就是像你那样点击绿色复选标记。我只是想把答案留给任何正在搜索答案的人。你知道,;)
VOID CALLBACK timerWork(PVOID lpParam, BOOLEAN TimerOrWaitFired /*ignored*/)
{
for (int ii = 0; ii < 10; ++ii) {
std::cout << "timer work: " << ii << std::endl;
Sleep(250);
}
}
int main(int argc, char* argv[])
{
HANDLE hTimerQueue = CreateTimerQueue(hTimerQueue);
if (NULL == hTimerQueue) return -1;
HANDLE hTimer = Start(hTimerQueue);
if (NULL == hTimer) return -1;
/* our timed callback is now running in the background */
int input = 0;
std::cin >> input;
while(input != -1)
{
if(input == 1)
{
if (Stop(hTimerQueue, hTimer)) {
std::cout << "Entered 1" << std::endl;
if (NULL == (hTimer = Start(hTimerQueue))) return -2;
}
}
else if(input == 2)
{
if (Stop(hTimerQueue, hTimer)) {
std::cout << "Entered 2" << std::endl;
if (NULL == (hTimer = Start(hTimerQueue))) return -2;
}
}
std::cin >> input;
}
DeleteTimerQueue(hTimerQueue);
return 0;
}