Search code examples
c++if-statementconditional-statementslvalue

Error lvalue required as left operand of assignment c++


The whole program basically just allows the user to move the cursor and if the user is in the given range of coordinates (2,2), the user is allowed to type the input. I have just provided some bits of the code which I thought would be enough for solving the problem.

I don't know what is causing this problem.Can you also explain why is it happening !!

void goToXY(int ,int);

Created a function with two ints.

int X = 0, Y = 0;

Initialised two ints.

if(X = 2 && Y = 2){
    cin >> input;
}

This is where the error is(it's above)

void goToXY(int x = 0, int y = 0) {

    COORD c;
    c.X = x;
    c.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

Here's where I define the function(it's above)


Solution

  • The problem is in this if statement

    if(X = 2 && Y = 2){
        cin >> input;
    }
    

    The condition is interpretated like

    if(X = ( 2 && Y ) = 2){
        cin >> input;
    }
    

    I think you mean

    if(X == 2 && Y == 2){
        cin >> input;
    }
    

    Take into account that it is better to include default arguments in the function declaration instead of the function definition because usually it is the declaration that is visible in a compilation unit.

    For example

    void goToXY( int = 0, int = 0 );
    

    Also you may redeclare the function in a block scope supplying another default arguments.