Cant seem to get this code to work.....

This is a discussion on Cant seem to get this code to work..... within the C Programming forums, part of the General Programming Boards category; Making a program for a school assignment but keep getting this error on my code..... "lvalue required as left operand ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    17

    Cant seem to get this code to work.....

    Making a program for a school assignment but keep getting this error on my code..... "lvalue required as left operand of assignment"

    This is my code
    Code:
    if (money > 1000){    money / 1000 = &thousand;
        money % 1000 = &remain;
        remain / 500 = ½
    }
    Please help out if you can!

    Alex

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    someVariable = &thousand means you are trying to assign the ADDRESS of the thousand variable, to someVariable. Same with &remain and &half.

    Looks like you wanted to work a math expression from left to right - but C works assignments, from right to left, in this case.

    An lvalue is some variable that could hold the value you're trying to give it. (short for left side of the equation variable value)

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Technically an lvalue does not have to be a variable (e.g. a macro corresponding to a memory-mapped register on some embedded system), but the vast majority of the time it is a variable.

    @Alex Coven:
    It would help if you gave us more context for your code and an explanation of what you're trying to do, otherwise it's hard for us to give good, complete help. I'm not clear on what you're trying to do, but I'm guessing you want to store the value of (money / 1000) in the variable thousand. Then, as Adak pointed out, your assignment is backwards, i.e. it should be:
    Code:
    variable_to_store_result = some_expression_of_the_right_type;
    Note that the & is definitely wrong, no matter what you're trying to do. But it's not clear, are thousand, remain and half output variables in a function? Did you mean the * dereference operator? Or are they just regular variables and you can drop the & all together?

    EDIT: Your other posts suggest you know how to do basic assignments, so what happened here?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just Can't Get This Code to Work
    By WMH in forum C Programming
    Replies: 20
    Last Post: 09-19-2012, 05:25 PM
  2. Why does this code work?
    By Whitewings in forum C Programming
    Replies: 8
    Last Post: 08-16-2011, 01:58 PM
  3. this code won't work
    By med linux in forum C Programming
    Replies: 7
    Last Post: 03-23-2011, 09:35 AM
  4. My Code Does Not Work? Why?
    By mintsmike in forum C Programming
    Replies: 8
    Last Post: 03-24-2009, 03:03 PM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 06:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21