• Register
1 vote
28 views

Problem:

I have a sample c++ code below that throws me back an error: “c++ expression must be a modifiable value” What could be the reason?

int A = 3; 
int B = 5; 
int match = 3;
for ( int k =0; k < B; k ++ )
{
    match --; 
    if ( match == 0 && k = A )
    {
         std::cout << " equals" << std::endl;
    }
}

Thanks for your help.

12 7 7
15,250 points

1 Answer

1 vote

 Solution:

You need to remember, the assignment operator has lower precedence than &&, so your condition is comparable as:

if ((match == 0 && k) = a)

But the left-hand side of this is an rvalue, namely the boolean resulting from the evaluation of the sub­expression match == 0 && k, so you cannot assign to it.

On the contrary, the comparison has higher precedence, so match == 0 && k == a is comparable to:

if ((match == 0) && (k == a))

I hope you get the point now.

13 9 6
94,240 points

Related questions

1 vote
2 answers 82 views
82 views
Problem: Hello all, How to declare an array properly in c/c++? I am a new student of c++ and I am trying to declare two-dimensional arrays in my program. Unfortunately, my arrays are not working. Here is my sample code int row = 5; int col= 5; int [row][col]; The above codes throw an error expression must have a constant value c++. What&rsquo;s wrong with it?
asked Apr 7 Gavin 15.3k points
0 votes
1 answer 6 views
6 views
Problem: I am working with loops. My query is that can I increment a foor loop by 2? If yes, then how? Please explain in detail and with syntax and examples because I am a newbie and have no experience in programming. Thanks
asked Oct 15 Sheeza 2.9k points
0 votes
1 answer 97 views
97 views
Problem : Looking for solution of following doubt if one for loop is nested within another, then the counter variables for the two loops should be
asked Nov 12, 2019 peterlaw 6.9k points
0 votes
1 answer 64 views
64 views
How c++ expression can have integral or unscoped type?
asked Sep 22 Daniel Anderson 4k points
1 vote
2 answers 683 views
683 views
Problem: I've got a problem with c++ code. I am attaching the sample code snippet below. Please pull me up. Yee, of course, I am pretty new in C++ #include <iostream> using namespace std; class Myclass { public: void myfunc() { cout<<"Hello World"<<endl; } }; int main ... = new Myclass(); // myfunc is part of the class object, rather than // part of the pointer to that class' object. a.myfunc(); }
asked Mar 7 Gavin 15.3k points