Dewang Gupta
Dewang Gupta

Reputation: 21

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)

Upvotes: 0

Views: 2096

Answers (2)

Arnav Borborah
Arnav Borborah

Reputation: 11779

You have a problem with:

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

Since you are using the wrong operator(=, not ==), then the precedence of 2 && Y becomes more. This is evaluated as:

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

Which, if true, would simplify to:

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

Which doesn't make sense because of true=2. Fix the code to:

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

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

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.

Upvotes: 6

Related Questions