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;
         }                                         
      }
   }                                                     
4

5 Answers 5

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.

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.

3
  • 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. Oct 18, 2012 at 21:13
  • To initialize i in the for loop as the value of a, I should use "=" instead of "=="? Oct 18, 2012 at 21:14
  • Correct. '==' is a check for equality. '=' is an assignment. Oct 18, 2012 at 21:15
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.

2
  • == has precedence over &&. Oct 18, 2012 at 21:02
  • @Cthulhu: You were right but worse than that, there was a missing close parenthesis. Fixed. Thanks! Oct 18, 2012 at 21:36
2

You cannot put spaces in the <= operator :

i < = b;

becomes

i <= b; 
1
  • 1
    It does matter on g++ ... I just tried
    – gogoprog
    Oct 18, 2012 at 21:31
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.