-1

I always get the error ((IntelliSense: expression must be a modifiable lvalue )) for the expression used with the "for" , please help .

for (c=2 ; c <= x -1 ; c++ )
    if ( x % c = 0 )
        cout << "not prime" ;
    else cout << "prime";
  • This is closed. But why everyone is missing one simple point: If number is not-divisible by any number - how it can be prime?? Let the test-loop finish first! – Ajay Mar 27 '13 at 05:34
  • @Ajay - a primer number is such, which is divisible only by one and by itself. What you understand by "prime" number? – Kiril Kirov Mar 27 '13 at 07:23
  • @KirilKirov - Let's say number being checked is 9.. So, by the loop, it is treated as prime. Consider another number 25, which is treated prime 3 times. Get what I mean? (Yes, of course, with `c%x` test) – Ajay Mar 27 '13 at 07:25
  • See this: http://stackoverflow.com/questions/15574648/sum-of-primes-under-2000000/15584486#15584486 – Ajay Mar 27 '13 at 07:29
  • @Ajay - yeah, I know how to implement a check if a number is prime ;) I misunderstood your remark. Nobody noted this, because this is not what the OP's asking - that's a different (logical) issue and has nothing to do with this error here. – Kiril Kirov Mar 27 '13 at 07:36
  • @KirilKirov, I partially agree with you. It's just because everyone is in hurry to give answers, and ignore details. It is very much relevant! There are hundreds of posts which give corrections about the mistake made, and this one the most trivial! – Ajay Mar 27 '13 at 07:39

6 Answers6

6

I guess you're trying to compare the result of x % c with 0. In this case,

if ( x % c = 0 )

This must be

// --------v
if ( x % c == 0 )

Note the additional =.


The reason is, that x % c does not return modifiable lvalue and using only one =, you're trying to assing 0 to the result of x % c, which is wrong.

Kiril Kirov
  • 36,509
  • 22
  • 109
  • 183
2

As far as I can tell you probably meant == instead of = here:

if ( x % c = 0 )

so like this:

if ( x % c == 0 )

In the first case you are trying to assign to result of an % operation which is a temporary and can not be assigned to. This previous thread covers some of the issues around temporaries. This article is a little more in depth but will probably give you a better understanding Understanding lvalues and rvalues in C and C++.

Community
  • 1
  • 1
Shafik Yaghmour
  • 148,593
  • 36
  • 425
  • 712
2

You're missing an = in the if line:

for (c=2 ; c <= x -1 ; c++ )
    if ( x % c == 0 )
        cout << "not prime" ;
    else cout << "prime";

(not that your code will tell you if a number is prime; it will print whether it's a multiple of every number smaller than it)

Chowlett
  • 44,716
  • 18
  • 112
  • 146
1

You're missing a =:

if ( x % c == 0 )
           ^^---- 

= is an assignment, == is an equality test.

Marc B
  • 348,685
  • 41
  • 398
  • 480
1

= is the assignment operator. What you're looking for is the comparison operator ==:

if ( x % c == 0 )
//         ^^

Live Demo

David G
  • 90,891
  • 40
  • 158
  • 247
0

I guess, you should look again at x % c == 0. You're assigning zero to the result of x modulo c.

For me your code works well as follows (Objective-C, so there's no @cout@, we use @NSLog();@:

for ( c = 2; c <= x-1; c++ ) 
    if ( x % c == 0 ) NSLog(@"not prime");
    else NSLog(@"prime");
igraczech
  • 2,246
  • 1
  • 24
  • 29