• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer

Mr.CodeHunter

Programming and Code Solutions

  • Home
  • C++
  • About Us
  • Contact Us
  • Privacy Policy

c++ expression must be a modifiable lvalue [SOLVED]

June 5, 2021 by Admin 2 Comments

Normally these type of c++ error occurs in conditional statments. For any conditional expression if we are using assignment operator(=) in-stand of comparison (==) then mostly c++ expression must be a modifiable lvalue error occurs.

1) c++ expression must be a modifiable lvalue (Scenario-1)

#include <iostream>
using namespace std;

int main()
{
    int A = 0; 
    int B = 1;
    int C = 1;

    // It is same as if ( (A + B) = C )
    if ( A + B = C ) // ERROR
    {
         std::cout << " Comparison match" << std::endl;
    }
        return 0;
}

Output:

error: lvalue required as left operand of assignment c++ expression must be a modifiable lvalue
error: lvalue required as left operand of assignment

Here we are assigning C variable to expression (A+B) so compiler is giving error of lvalue required as left operand of assignment. if we want to resolve this error then we have to use comparison (==) operator. Also always make sure to add brackets “(” and “)” for easy understanding and also to avoid any operator precedence errors.

SOLUTION :

    if ( (A + B) == C ) // SOLUTION using (==)
    {
         std::cout << " Comparison match" << std::endl;
    }

By modifying this code we can easily resolve lvalue required as left operand of assignment error from condition.

2) c++ expression must be a modifiable lvalue (Scenario-2)

#include <iostream>
using namespace std;

int main()
{
    int A = 0; 
    int B = 1;
    int C = 1;
    
    // Same as if ( (A == 0 && B) = C )
    if ( A == 0 && B = C ) // ERROR
    {
         std::cout << " Comparision match" << std::endl;
    }
        return 0;
}

Output:

c++ expression must be a modifiable lvalue
error: lvalue required as left operand of assignment

SOLUTION :

    if ( (A == 0) && (B == C) ) // SOLUTION using (==)
    {
         std::cout << " Comparision match" << std::endl;
    }

Here assignment(=) operator has lower precedence then &&. So if we change assignment operator to comparison (==) operator then it has higher precedence so (A==0) && (B==C) condition will work proper and expression must be a modifiable lvalue error will be resolved.

CONCLUSION:

Whenever your c++ compiler get error for expression must be a modifiable lvalue, you always need to check below points

  1. Check Assignment (=) operator is used in stand of comparison operator (==)
  2. Check Operator precedence for expression is correctly applied
  3. Try to use bracket for each expression and comparisons for avoiding mistakes

SEE MORE:

https://mrcodehunter.com/what-is-cpp-used-for-real-world-applications/

Filed Under: All C++, C++

Reader Interactions

Comments

  1. Jason Bartelds says

    December 17, 2021 at 4:20 am

    #include

    int main() {
    int x;
    std::cin >> x;
    std::cout << "pick a number";
    int a;
    x * 2 = a;
    std::cout << "now its " << a;

    }

    Reply
    • Admin says

      January 15, 2022 at 5:51 am

      Hello Jason,
      Can you give more detail. Are you facing any issue or error ?

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search here

Social Media

  • Facebook
  • YouTube

SEE MORE

Fibonacci sequence c++

Fibonacci Sequence c++ is a number sequence which created by sum of previous two numbers. First two number of series are 0 and 1. And then using these two number Fibonacci series is create like 0, 1, (0+1)=1, (1+1)=2, (2+1)=3, (2+3)=5 …etc Displaying Fibonacci Series in C++ ( without recursion) Output: From given output we […]

map c++

C++ Map [Learn by Example]

C++ map is part of Standard Template Library (STL). It is type of Associative container. Map in c++ is used to store unique key and it’s value in data structure. But if you want to store non-unique key value then you can use Multi Map in c++. Let us first understand in detail what is […]

how to copy paste in turbo c++ ?

There are many different C++ IDE are available but still many students are using Turbo c++ for learning c/c++ programming languages. During using Turbo c++ if you are beginner you will be confuse for how to copy and paste in turbo c++ or if you have already copy some content and you want to paste […]

C++

return 0 c++

There are two different scenario return statement is used inside c++ programming. We can use return 0 c++ inside main() function or other user defined functions also. But both have different meanings. return 0 c++ used inside Main function return 0 c++ used inside other user defined function What is meaning of return 0 and […]

C++

c++ expected a declaration [ SOLVED]

When any function or statement is not in scope or we have used wrong syntax then possibly you will get error for c++ expected a declaration in your code. Main reasons for errors are: Incorrect use/declaration inside namespace Statements are added out of scope Required statement need to add inside main/function Solution-1 | Expected a […]

C++

c++ cannot open source file “errno.h” [SOLVED]

Normally you will face c++ cannot open source file “errno.h” in MS Visual Studio c++ projects. These are some solutions to remove opening errors for “errno.h” file. I got the errors to go away by installing the Windows Universal CRT SDK component, which adds support for legacy Windows SDKs. You can install this using the […]

Footer

DISCLAIMER

The information contained on https://www.mrcodehunter.com is for general information purposes only. We assumes no responsibility for errors or omissions in the contents on the Service.

SITEMAP XML

Sitemap

Recent

  • Fibonacci sequence c++
  • C++ Map [Learn by Example]
  • how to copy paste in turbo c++ ?
  • return 0 c++
  • c++ expected a declaration [ SOLVED]

Search

Copyright © 2025 ยท Mr Code Hunter