CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008

    Unhappy Making a simple calculator :error C2106: '=' : left operand must be l-value

    Hi I'm making a simple calculator program and I've encountered errors that I've never heard of before. No matter what I do, I don't know how to fix this problem

    error C2106: '=' : left operand must be l-value

    What does that mean? I don't know what I did wrong.
    I am using Visual Studio 2008
    I haven't done programming in a while so I'm kinda rusty... Please help!
    thank you

    #include <string>
    #include <iostream>
    #include <iomanip>
    using namespace std;

    void main ( )
    int choice;
    double a, b, c;
    char an;
    cout << "Please enter the operation you want" << endl;
    cout << "Press 1 for addition" << endl << "Press 2 for subtraction" <<
    endl << "Press 3 for division" << endl << "Press 4 for multiplication"
    << endl;
    cin >> choice;
    cout << "Please enter the numbers you want to do calculation with" << endl;
    while (an = 'y')
    if (choice == 1)
    a + b = c;
    if (choice == 2)
    a - b = c;
    if (choice == 3)
    a / b = c;
    if (choice == 4)
    a * b = c;
    cout << "Here is your result: " << c << endl;
    cout << "want to do it again?" << endl;
    cin >> an;

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Seattle, WA

    Re: Making a simple calculator :error C2106: '=' : left operand must be l-value

    I see two problems there: Your assignments are backwards (= assigns from right to left, *not* the other way around), and your while loop condition is an assignment when you probably want it to be a comparison.

    Well, three problems if you count the fact that you didn't use code tags.

    An "lvalue" is an expression which corresponds to a memory location: you can put a value there, so it can be a value on the left of an = assignment. An "rvalue" can be any arbitrary expression of the correct type; there is no need for it to correspond to a single memory location, because it's going to be assigned to an lvalue since it's on the right of an = assignment.
    Last edited by Lindley; December 2nd, 2008 at 10:31 PM.

  3. #3
    Join Date
    Nov 2008

    Re: Making a simple calculator :error C2106: '=' : left operand must be l-value

    An lvalue is basically something that can be assigned to, an rvalue is basically a temporary.

    in your code, you have lines like...

    a+b = c;

    a+b makes a temporary object that holds the result of a+b, as its a temporary, its an rvalue and thus cannot be assigned to.


    would work fine.

    c is a named variable thats not const, it can be assigned to hence its an lvalue. So now we take the same temporary you were making before then assign that result to c.

    lvalue and rvalue were terms that originated for describing something that can be on the left hand side of the assignment operator and something that can only be on the right hand side of an assignment operator.

  4. #4
    Join Date
    Dec 2008

    Re: Making a simple calculator :error C2106: '=' : left operand must be l-value

    oh okay. thank you. it works very well now.
    haha I forgot for a minute that it goes right to left

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.