“lvalue required as left operand of assignment ” error

后端 未结 4 1937
清歌不尽
清歌不尽 2020-12-10 20:36

The following code produces a \"lvalue required as left operand of assignment\"

if( c >= \'A\' && c <= \'Z\'  || c = \" \" || c = \",\") {


        
相关标签:
4条回答
  • 2020-12-10 21:11

    Personally, I prefer the minimalist style:

    ((x == 'c' && y == 'b') || (z == ',') || (z == ' '))
    
    (  x == 'c'  &&  y == 'b'   ||   z == ','   ||   z == ' '  )
    

    or

    (  x == 'c'  &&  y == 'b'  ?  z == ','  :  z == ' '  )
    

    against

    ( x == 'c' && y == 'b' ? z == ',' : z == ' ')
    
    0 讨论(0)
  • 2020-12-10 21:13

    You should use single quotes for chars and do double equals for equality (otherwise it changes the value of c)

    if( c >= 'A' && c <= 'Z'  || c == ' ' || c == ',') {
    

    Furthermore, you might consider something like this to make your boolean logic more clear:

    if( (c >= 'A' && c <= 'Z')  || c == ' ' || c == ',') {
    

    Although your boolean logic structure works equivalently (&& takes precedence over ||), things like this might trip you up in the future.

    0 讨论(0)
  • 2020-12-10 21:23

    = is the assigning operator, not the comparing operator. You are looking for ==.

    0 讨论(0)
  • 2020-12-10 21:30

    equality is ==, = is assignment. You want to use ==. Also "" is a char*, single quotes do a character.

    Also, adding some parens to your condition would make your code much easier to read. Like so

     ((x == 'c' && y == 'b') || (z == ',') || (z == ' '))
    
    0 讨论(0)
提交回复
热议问题