2

I want to do the coding like enter your user name and password, and then it will tell you succeed or not.

Here is my code:

#include<iostream>
using namespace std;
int main()
{
    const int USR = 201910, PSW = 765705590;
    int user, psw;
    cout << "user:";
    cin >> user;
    cout << "password:";
    cin >> psw;
    if(user == USR && psw = PSW) // 2 errors: 1. E1037 expression must be a modifiable lvalue 2. '='left operand must be l-value
    {
        cout << "welcome to US bank!";
    }
    else if (user != USR || psw != PSW)
    {
        cout << "password  or username is wrong!";
    }
}

I am a comer in C++, could you help me to figure out this two errors? thanks!

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
waltson
  • 21
  • 4

1 Answers1

4

if(user == USR && psw = PSW) // 2 errors: 1. E1037 expression must be a modifiable lvalue 2. '='left operand must be l-value

The error is here: psw = PSW

Going by the operator precedence,

user == USR && psw = PSW

becomes

((user == USR) && psw) = PSW

Now see ((user == USR) && psw). What do you think the result of this expression would be? It is either true or false, which is not an lvalue.

What are rvalues, lvalues, xvalues, glvalues, and prvalues?

In simple words, what are you assigning PSW to? It makes no sense because the LHS of that expression is not a variable.

Solution:

You may be surprised by these errors because you actually just wanted to compare if psw is equal to PSW, but you accidentally used that assignment operator.

Replace psw = PSW with psw == PSW.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53