Thread: Error: I value required as left operand of assignment

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    Error: I value required as left operand of assignment

    I am getting this error when compiling my program with quincy:
    Error: I value required as left operand of assignment
    Tthe program is meant to calculate how much parking costs based on the amount of hours in a park and what type of vehicle it is. the error is coming from my function definitions which i have just started to add in.
    Code:
    float calcCarCost (char vehicletype, int time, float car)
    {
    
     if ((time > MINTIME) && (time <= 3)) 
     calcCarCost =( CAR * time );
     
         
    }
    the error is on line 72 which is:
    calcCarCost =( Car * time);

    i should probably point out CAR is already defined as a constant with a numerical value given and time is previously asked to be input in when the program runs.

    i am pretty new to programming in general which is probably pretty obvious by now and im just wondering what is causing the problem. If any more information is needed just ask. Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    float calcCarCost (char vehicletype, int time, float car)
    {
    
     if ((time > MINTIME) && (time <= 3)) 
     calcCarCost =( CAR * time );
     
         
    }
    You're trying to assign to your function name.

    ( CAR * time ) isn't valid syntax either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Look up the keyword return; it is used to return a value from a function in C and C++.

    Edit: "I value" was likely "lvalue" with the first letter an L standing for "left value".

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    First, variables are typically passed into a function for a reason... such as they are going to be used in some way by said function. You have several arguments being passed into the function and only one of them (time) seems to be used. What is the point of the other variables? Do they really need to be passed into the function if they aren't going to be used? Or, is the function incomplete?

    Second, be aware that the language is case-sensitive. In your post you have a function argument car (all lower case), you have a variable CAR in the function body, and you have Car that is mentioned in your post. Which one is which? Is there a typo somewhere? The language rules would treat each of these as separate variables so you need to make sure you know what you're talking about.

    Thirdly, your function as written will only return a value when the condition (time > MINTIME) && (time <= 3) is met. This means that the compiler should complain with a message along the lines of "not all control paths return a value" since nothing will be returned from the function if your time argument is outside the range of (MINTIME,3].
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Salem View Post
    ( CAR * time ) isn't valid syntax either.
    Well lexically that's just the syntax for multiplication, and it's the lack of being able to successfully look up the symbol for CAR that is the semantic problem.

    Jeffery1: C is CaSe sEnSiTiVe!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Well lexically that's just the syntax for multiplication, and it's the lack of being able to successfully look up the symbol for CAR that is the semantic problem.
    O_o

    This is C++; the statement in isolation can be almost anything and imply anything at all. Here, if you missed the note that `CAR' is a constant numeric value, the syntax is almost certainly invalid. (You also apparently missed the note because `CAR' and `car' are said to be intentionally two different things.) It can easily be assumed that `CAR' is a type also implying problems with case consistency or misunderstanding how to return an instance of a `CAR' object.

    [Edit]
    I'll explain for those who don't appreciate the problems of this form of mixed declarations and expressions.

    That is the syntax for a few different things. (Most obviously, pointer declaration syntax and the multiplication operator.)

    What it is semantically depends on "LHS" and "RHS".

    If `CAR' is a type, the syntax is a declaration and semantically you've declared a pointer to a `CAR' type. (With C++, it doesn't matter what `time' is in this case because the compiler will try to treat it as a declaration.)

    If `CAR' is a variable and `time' is a variable or literal alias, the syntax is the multiplication operator and semantically you've done whatever is appropriate to the types involved. (So, preferably, some form of multiplication is involved.)

    If `CAR' is a variable and `time' is not a variable or literal alias, the syntax is meaningless and the result semantically meaningless.

    This all before templates and the C inherited preprocessed are involved, but at that point you have to expand everything so isn't interesting to this case. To be clear, what the expression may actually be can depend on how the template was instantiated or what problems may be related to macro.
    [/Edit]

    Soma

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Ugh, my head is pounding. Should be very careful what I post when I'm sick.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by phantomotap
    This is C++
    Actually, this is C. Yeah, I know, easy to forget which forum you're in around here. Maybe we need a different colour scheme or something.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Yeah, I know, easy to forget which forum you're in around here. Maybe we need a different colour scheme or something.
    O_o

    I guess I need to confess that I tripped over myself.

    I get tired of writing out the relevant C family so just listed C++.

    When I came back to explain I thought it was the C++ board because of my own post. ;_;

    Soma

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    yeah thanks i fixed all the problems and it works fine now. Thanks to everyone who gave some input

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Different colours might be fun
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: lvalue required as left operand of assignment
    By rocket_c in forum C Programming
    Replies: 4
    Last Post: 02-11-2013, 12:33 AM
  2. Replies: 10
    Last Post: 11-27-2012, 11:54 AM
  3. error: lvalue required as left operand of assignment
    By owjian1987 in forum C Programming
    Replies: 5
    Last Post: 02-11-2011, 12:34 PM
  4. Replies: 3
    Last Post: 06-01-2010, 06:22 AM
  5. !value required as left operand of assignment
    By Jasper in forum C Programming
    Replies: 4
    Last Post: 08-15-2009, 02:21 PM