2

Possible Duplicate:
lvalue required as left operand of assignment
lvalue required as left operand of assignment (C program)

I am getting this error on my code and I am not sure why. Please help! The error is:

error: expected primary-expression before ‘=’ token

This is the part of my code i'm having the issue with. a and b were already previously declared as ints.

   int i = 0;
   for( i == a; i < = b; i = i + 1)  // ERROR IS IN THIS LINE
   {  int j = 1;
      int N = static_cast<int>(sqrt(i));

      for( j = 1; j < = N; j = j + 1)   // ERROR IS IN THIS LINE
      {  int P = i%j;
         if( P == 0 && j!= 1 && j!= i)
         {  j = N + 1;
         }
         if( P != 0 && j == N)
         {  cout << i << "is prime" << endl;
         }                                         
      }
   }                                                     
Community
  • 1
  • 1
user1757575
  • 105
  • 2
  • 2
  • 5
  • 1
    That's equivalent to `if( (P != 0 && j) = sqrt(i))`. – chris Oct 18 '12 at 20:55
  • This has to be a duplicate. That said, it shows poor research effort and I'm not sure why it's getting upvotes. – Wug Oct 18 '12 at 20:57
  • This question is not quite exactly the same but has exactly the same answer. [lvalue required as left operand of assignment](http://stackoverflow.com/questions/6162438/lvalue-required-as-left-operand-of-assignment) – Wug Oct 18 '12 at 21:01
  • This one is a much closer match: [lvalue required as left operand of assignment (C program)](http://stackoverflow.com/questions/8570020/lvalue-required-as-left-operand-of-assignment-c-program) – Raymond Chen Oct 18 '12 at 21:03

5 Answers5

3

I think you meant to do

if( P != 0 && j == sqrt(i))

= is the assignment operator, == tests for equivalence and returns boolean.

The assignment operator has lesser precedence than logical AND (&&), so it being read as

(P != 0 && j) = sqrt(i)

which is why the lvalue error is being reported.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
2

Because of the low precedence of assignment, what you wrote:

if (P != 0 && j = sqrt(i))

is interpreted as:

if ((P != 0 && j) = sqrt(i))

and you can see that is not an l-value. If you want to assign the square root of i to j, you probably should write:

if (P != 0 && (j = sqrt(i)) != 0.0)

assuming that j is a floating point variable; if it's an integer type, use 0 in place of 0.0. Otherwise, you should write:

if (P != 0 && j == sqrt(i))

which does an explicit equality comparison.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
2

You cannot put spaces in the <= operator :

i < = b;

becomes

i <= b; 
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
gogoprog
  • 613
  • 4
  • 7
2

I'll play along. The 'error' is the same in both case:

for( i == a; i < = b; i = i + 1)  // ERROR IS IN THIS LINE
...
for( j = 1; j < = N; j = j + 1)   // ERROR IS IN THIS LINE

The error is the '< =' which is not an operator and not valid C or C++. Sidenote, the 'i == a' in the first loop is a no-op.

Nik Bougalis
  • 10,495
  • 1
  • 21
  • 37
  • I mean it has no side-effects, or to put it simply: it does nothing. It compares i to a. If they're the same, it returns 'true', otherwise it returns 'false'. But the return value is discarded and not used. – Nik Bougalis Oct 18 '12 at 21:13
  • To initialize i in the for loop as the value of a, I should use "=" instead of "=="? – user1757575 Oct 18 '12 at 21:14
  • Correct. '==' is a check for equality. '=' is an assignment. – Nik Bougalis Oct 18 '12 at 21:15
1

Write the condition as follows.

if( (P != 0) && (j == sqrt(i)) ) 

instead of

if( P != 0 && j = sqrt(i))

This = sign is assignment operator, not a comparison operator. You need to check if, the left hand side is equal to the right hand side.

Coding Mash
  • 3,338
  • 5
  • 24
  • 45