Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"lvalue required as left operand of assignment " error

Tags:

c

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

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

I assume I'm writing this wrong, what is wrong? and how would I write it correctly?

like image 757
Skizit Avatar asked Dec 03 '22 05:12

Skizit


2 Answers

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.

like image 169
Michael Chinen Avatar answered Dec 26 '22 15:12

Michael Chinen


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 == ' '))
like image 32
Paul Rubel Avatar answered Dec 26 '22 15:12

Paul Rubel