我刚来这地方。我的C++大学类(class)结束后有点使用rust 。我正在尝试制作一个程序,其中特定的击键记录了cout中看到的文本。我遇到的问题是计时器函数阻止主函数继续运行。设置计时器函数是为了使每次延迟达到28时,将 Tic 增加1。计时器循环并达到28时,延迟重置为0。为什么不是我的ojit__strong继续吗?是否正在等待计时器完成循环?如何使计时器和主同时工作?这不是家庭作业。制作个人项目。
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
using namespace std;
int Tic = 0;
int Delay = 0;
bool KeyIsListed(int iKey)
{
switch (iKey)
{
case 0x41:
cout << "ACS_Execute(10,0,0,0,0);\n"; //the A Note
cout << "Delay("<<Tic<<");\n"; //Capture the time
Delay = 0;
Tic = 0
break;
case 0x53:
cout << "ACS_Execute(11,0,0,0,0);\n"; //the S Note
cout << "Delay("<<Tic<<");\n"; //Capture the time
Delay = 0;
Tic = 0
break;
case 0x44:
cout << "ACS_Execute(12,0,0,0,0);\n"; //the D Note
cout << "Delay("<<Tic<<");\n"; //Capture the time
Delay = 0;
Tic = 0
break;
case 0x46:
cout << "ACS_Execute(13,0,0,0,0);\n"; //the F Note
cout << "Delay("<<Tic<<");\n"; //Capture the time
Delay = 0;
Tic = 0
break;
case 0x47:
cout << "ACS_Execute(14,0,0,0,0);\n"; //the G Note
cout << "Delay("<<Tic<<");\n"; //Capture the time
Delay = 0;
Tic = 0
break;
case 0x48:
cout << "ACS_Execute(15,0,0,0,0);\n"; //the H Note
cout << "Delay("<<Tic<<");\n"; //Capture the time
Delay = 0;
Tic = 0
break;
case 0x4A:
cout << "ACS_Execute(16,0,0,0,0);\n"; //the J Note
cout << "Delay("<<Tic<<");\n"; //Capture the time
Delay = 0;
Tic = 0
break;
case 0x4B:
cout << "ACS_Execute(17,0,0,0,0);\n"; //the K Note
cout << "Delay("<<Tic<<");\n"; //Capture the time
Delay = 0;
Tic = 0
break;
case 0x4C:
cout << "ACS_Execute(18,0,0,0,0);\n"; //the L Note
cout << "Delay("<<Tic<<");\n"; //Capture the time
Delay = 0;
Tic = 0
break;
}
}
int timer()//Doom Tic timer
{
while(TRUE)
{
if(Delay == 28)//Aprox to 1 tic
{
Delay = 0;//Reset delay to 0
Tic ++;//Increase Tic by 1
}
else
{
Delay ++;//Increase Delay until it is at 28
}
Sleep(0.1);
}
}
int main()
{
char key;
timer();//Call timer Function (This is preventing the main function from continuing)
while(TRUE)
{
for(key = 8; key <= 190; key ++)
{
if(GetAsyncKeyState(key) == 1)
{
if(KeyIsListed(key) == FALSE)
{
}
}
}
}
return 0;
}
最佳答案
while(TRUE)
{
if(Delay == 28)//Aprox to 1 tic
{
Delay = 0;//Reset delay to 0
Tic ++;//Increase Tic by 1
}
else
{
Delay ++;//Increase Delay until it is at 28
}
Sleep(0.1);
}
这是不可能的。您是要在
break
块中放入if
吗?或while (Delay > 0)
(最初设置为1)。另外,如注释中所述,如果您说一个函数返回int,则需要确保它在每个路径上都有一个return语句。也许不是
break
,而是只想要return Tic;
?关于c++ - 功能计时器防止电源继续,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51529123/