功能计时器阻止主功能继续

Function timer prevents main from continuing

本文关键字:功能 继续 计时器      更新时间:2023-10-16

我是新来的。上完C++大学课程后有点生疏。我正在尝试制作一个程序,其中特定的按键记录在cout中看到的文本。我遇到的问题是计时器功能阻止函数继续运行。每当延迟达到28时,计时器功能会使Tic增加1计时器循环,当它达到28时,延迟重置回0。为什么我的main没有继续?是否可能是在等待计时器完成循环?如何使计时器同时工作?这不是家庭作业。制作个人项目。

#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);
}

没有办法摆脱这个循环。你的意思是在if块中放一个break吗?或者while (Delay > 0)(初始设置为1(。

此外,正如注释中所提到的,如果说函数返回int,则需要确保它在每个路径上都有一个return语句。也许你只想return Tic;而不是break