How to avoid this error message? "main.cpp:8:16: error: lvalue required as left operand of assignment"

Asked

Viewed 488 times

-2

I’m getting the error message: "main.cpp: In Function 'int main()': main.cpp:8:16: error: lvalue required as left operand of assignment (-10))/76=n-1;"

In the code snippet below,?

#include 
using namespace std;
int main(){
  int ano;
  cin>>ano;
  int passagem;
  int n;
  (ano-10)/76=n-1;
  passagem=(10+76)*(n-1);

  }'

  • Are you trying to use the equal sign that is used to define variable values to set the value to another value? This doesn’t make much sense (ano-10)/76=n-1;, the sign of = is used to set values in variables, for example int a = n-1;, does things like 1+1=5+2; it wouldn’t make sense. What exactly are you trying to do?

  • 2

    It seems to me an undue mixture of math and programming. The equality of mathematics has to be converted to attribution, so that the variable is on the left of the = (in mathematics the = is equality. in most languages the = is attribution, as answered by @Guilherme). Example: x-7=12 has to be "converted" to x = 12 + 7. another example: 7 - y = n * 12 have to turn n = ( 7 - y ) / 12, if you already have the y and wants to get the n.

1 answer

1


The sign of equal = is used to set values in variables, when you do:

(ano-10)/76=n-1;

You try to set the value of n-1 for the value of (ano-10)/76, but this has no meaning and cause failure, the equal sign should be used with variables, as for example:

int foo = n-1;
int bar = (ano-10)/76;

But it depends a lot on what you want to do, there is also the == which is the sign of comparison, for example:

if (foo == bar) {
     //Acontece algo
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.