功能计时器防止电源继续

时间:2018-07-25 23:43:33

标签: c++ loops keystroke

我是新来的。我的C ++大学课程结束后有点生锈。我正在尝试制作一个程序,其中特定的击键记录在cout中看到的文本。我遇到的问题是计时器功能阻止了 main 功能的继续。每次 Delay 达到28时,都会使用 timer 功能将 Tic 增大1。 timer 循环播放以及达到该时间时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;

}

1 个答案:

答案 0 :(得分:3)

  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;