Max Yari
Max Yari

Reputation: 3707

Defining variable in function through if else

Please see the small code below:

void TestPluginAPI::MouseMove(int nX, int nY)
{
    INPUT input;
    DOUBLE fScreenWidth = GetSystemMetrics( SM_CXSCREEN )-1; 
    DOUBLE fScreenHeight  = GetSystemMetrics( SM_CYSCREEN )-1; 
    int plusY;
    if (FullScreenCheck() == 1) {int plusY = getmidY() + 8.0f;}
    else {int plusY = getmidY();}
    int plusX = getmidX();
    DOUBLE fX = plusX*(65535.0f/fScreenWidth) + (nX*(65535.0f/fScreenWidth));
    DOUBLE fY = plusY*(65535.0f/fScreenHeight) + (nY*(65535.0f/fScreenHeight));

    RtlZeroMemory(&input, sizeof(input));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
    input.mi.dx = (LONG)fX;
    input.mi.dy = (LONG)fY;

    SendInput(1, &input, sizeof(input));
}

The thing is that the compiler gives me a warning, that plusY is undefined, and obviously if I try to run this function in a compiled plugin (it's from plugin code, debugging it thru javascript console in a browser) it crashes because plusY is undefined.

It seems that if I can't define variables in function through if-else, then how can I do this?

Upvotes: 0

Views: 1146

Answers (5)

Code-Apprentice
Code-Apprentice

Reputation: 83567

int plusY;

You have declared a variable named plusY which is available to your entire function

if (FullScreenCheck() == 1) {int plusY = getmidY() + 8.0f;}

This declares another variable which also happens to be named plusY, but this new variable is only available inside of the if statement.

else {int plusY = getmidY();}

And now you declare a third variable with the same name which is only available in the else clause.

To fix this and use the same variable in all locations, change your code to

int plusY;
if (FullScreenCheck() == 1) {plusY = getmidY() + 8.0f;}
else {plusY = getmidY();}

p.s. As an aside, you should learn about typical code formatting practices and conventions.

p.p.s. You should also initialize plusY to a value which is otherwise invalid so you can easily track down any problems. Alternatively, you can use the ternary operator in order to avoid this issue entirely. In this situation, the ternary operator is most likely the best solution.

Upvotes: 1

James Kanze
James Kanze

Reputation: 154007

Variables defined in one of the branches of an if have the scope of that branch only. Even if you eliminated the braces (which would be legal here):

if ( fullScreenCheck() == 1 )
    int plusY = getmidY() + 8.0f;
else
    int plusY = getmidY();

will result in a plusY whose scope ends after the if.

The best way of handling this is:

int plusY = fullScreenCheck() == 1
            ? getmidY() + 8.0f
            : getmidY();

Because of the restrictions on what you can do in a single expression, it's not always possible to do it, but whenever you can, it results in far more readable code.

Upvotes: 2

Drew Dormann
Drew Dormann

Reputation: 63912

Your entire if/else can be replaced (with scoping rules fixed) with this one line.

int plusY = (FullScreenCheck() == 1) ? getmidY() + 8.0f : getmidY();

Upvotes: 1

David G
David G

Reputation: 96845

plusY is destructed from memory at the closing brace of the if statement. If you need to use its after the if statement then you should declare it outside the block:

int plusY;

if (...)
{
    plusY = getmidY() + 8.0f;
}

Upvotes: 1

user1944441
user1944441

Reputation:

{int plusY = getmidY() + 8.0f;}

int plusY stops existing at the } in both if statements

int plusY inside brackets {} is a different variable than int plusY outside.

You should do this

int plusY;
if (FullScreenCheck() == 1) {plusY = getmidY() + 8.0f;}
else {plusY = getmidY();}

Upvotes: 4

Related Questions