简体   繁体   English

C ++多重逻辑运算符

[英]C++ Multiple Logical Operator

I'm rather new to C/C++. 我是C / C ++的新手。 I have a segment of my application which doesn't seem to work as I'd want but I cannot understand why. 我的应用程序的某个部分似乎无法正常运行,但我不明白为什么。

What I'm looking to do is when the 4 key is in the status of down, I'd like it to carry out the 'idle' function. 我想做的是当4键处于按下状态时,我希望它执行“空闲”功能。 I'd like the idle function to have 2 outcomes. 我希望空闲函数有2个结果。 If the Up OR Down OR Left OR Right OR LMouse AND RButton then carry out the 'movement rotation operation' code else just carry out the standard idle function. 如果向上或向下或向左或向右或LMouse和RButton然后执行“运动旋转操作”代码,否则只需执行标准的空闲功能。 However within my code, it'll loop this while it's down but the moving() will only ever return 0 但是在我的代码中,它会在关闭时循环循环,但是move()只会返回0

I've been messing with it for some time and trying to look on google for answers but I cannot understand why. 我已经弄乱了一段时间,试图在google上寻找答案,但我不明白为什么。

Here's my segment of code: 这是我的代码段:

int moving()
{
    int u = GetAsyncKeyState(VK_UP);
    int d = GetAsyncKeyState(VK_DOWN);
    int l = GetAsyncKeyState(VK_LEFT);
    int r = GetAsyncKeyState(VK_RIGHT);
    int mr = GetAsyncKeyState(VK_RBUTTON);
    int ml = GetAsyncKeyState(VK_LBUTTON);
    if(u == 1 || d == 1 || l == 1 || r == 1 || mr == 1 && ml == 1)
    {
        return 1;
    }
}

void idle()
{
    cout << "moving = " << moving() << endl;
    if(moving() == 1)
    {
        cout << "Movement rotation operating." << endl;
    }
    else
    {
        cout << "This is the idle statement" << endl;
    }

}

int main()
{
    while(1)
    {
    if(GetAsyncKeyState('4'))
        {
            cout << "4 Pressed" << endl;
            idle();
        }
    }
}

Thank you in advance. 先感谢您。

Your logic to determine the button combination needs an extra set of parentheses. 确定按钮组合的逻辑需要额外的括号。

if(u == 1 || d == 1 || l == 1 || r == 1 || (mr == 1 && ml == 1))

Also, 1 will evaluate to true so you can say 此外, 1计算结果为true因此您可以说

if(u || d || l || r || (mr && ml))

You could also make the function return a bool since that is really what you're after. 您还可以使函数返回bool因为这实际上是您要执行的操作。

bool moving()
{
    // ...
    // code for getting button states
    // ...
    return (u || d || l || r || (mr && ml))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM