Home > Back-end >  lvalue required as left operand of assignment 2
lvalue required as left operand of assignment 2

Time:12-02

#include <iostream>
using namespace std;
int main()
{
    int g1, m1, s1, g2, m2, s2, x, y, z;
    cin >> g1 >> m1 >> s1 >> g2 >> m2 >> s2;
    x=g1 g2;
    y=m1 m2;
    z=s1 s2;
    if(m1 m2>59) x 1 && y=y-60;
    if(s1 s2>59) y 1 && z=z-60;
    cout << x << y << z;
}

I'm new to c and don't know how to fix it, can someone help me?

CodePudding user response:

The problem is that assignment operator = has the lowest precedence in your expressions:

if(m1 m2>59) x 1 && y=y-60;
if(s1 s2>59) y 1 && z=z-60;

Thus, the compiler sees the expressions like this:

(x   1 && y) = (y - 60);
(y   1 && z) = (z - 60);

And the result of (x 1 && y) and (y 1 && z) cannot be assigned, because it's an rvalue.

Instead you probably want the assignment to take place prior to evaluating result of &&:

if(m1   m2 > 59) x   1 && (y = y - 60);
if(s1   s2 > 59) y   1 && (z = z - 60);
  • Related