Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

"lvalue required as left operand of assignment" for all of my if/else statements. What have I done wrong?

I’m making a basic calculator that uses if/else statements to select a sign to use in the equation. All of the else/if statements are coming back with "error: lvalue required as left operand of assignment."


    #include <iostream>
using namespace std;

int main()
{
    
//declare variables

    int num1;
    int num2;
    int sol;
    bool sign;

    //prompt for num1
    cout << "Greetings! Please enter a number." << endl;
    cin >> num1;

    //prompt for num2
    cout << "Great! Now, please enter the second number." << endl;
    cin >> num2;

    //prompt for sign
    cout << "Almost there! Now, please enter the sign you wish to use in the equation." << endl;
    cin >> sign;

    //arguments
    if (sign == '+')
    {
        num1 + num2 = sol;
    }
    else if (sign == '-')
    {
        num1 - num2 = sol;
    }
    else if (sign == '*')
    {
        num1 * num2 = sol;
    }
    else if (sign == '/')
    {
        num1 / num2 = sol;
    }
    else
    {
        cout << "Sorry, that is not a valid symbol." << endl;
        return 0;
    }

    //print results
    cout << "The solution is: " << sol << endl;
    return 0;

}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You have your assignments reversed. = is not a commutative operator.

They should all be of the form

sol = num1 op num2

What you are trying to do is assign the (uninitialised) value of sol to the unnamed temporary that results from the calculation. Happily this is a hard error with fundamental types.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading