101001
101001

Reputation: 111

Expression not assignable (Calculator without using * or /)

I have to create a calculator but without using * or / within the code. I keep on getting an

"Expression not assignable"

error while trying to loop using addition for conducting multiplication. Any suggestions?

char op;
int lhs, rhs; // stands for left-hand and right hand side of user input expression
int i;
int lhsnew;
int lhs2;

cout << "Enter the expression to run the calculator simulator: " << endl;
cin >> lhs >> op >> rhs;
//left hand side(lhs) operator right hand side (rhs)

switch(op)
// Switch based on operator chosen by user
{
    case'+':
    {
        cout << "The result is " << lhs + rhs << endl;
        break;
    }
    case'-':
    {
        cout << "The result is " << lhs - rhs << endl;
        break;
    }
    case'*':
    {
    // Here I made the loop control variable same as the rhs number submitted by user
        while(i >= rhs)
        {
            lhsnew = 0;
            lhsnew + lhs = lhsnew;

            i++;
            // Should I use a different looping method?
        }
        cout << "The result is " << lhsnew;`enter code here`
        break;
    }

    // Haven't included case for division
    return 0;
}

Upvotes: 1

Views: 118

Answers (1)

john
john

Reputation: 87997

lhsnew + lhs = lhsnew;

should be

lhsnew = lhsnew + lhs;

You just got it backwards I guess. But then why did you write the correct

lhsnew = 0;

instead of the incorrect

0 = lhsnew;

in C++ what you are assigning goes on the right hand side, and what you are assigning to (usually a variable) goes on the left hand side.

Also note that your loop is very wrong, it should be

    lhsnew = 0;
    i = 0;
    while (i < rhs)
    {
        lhsnew = lhsnew + lhs;
        i++;
    }

1) You only want to assign zero to lhsnew once, so it should go before the loop, not inside the loop.

2) You never gave i a value before you used it, it needs to start at zero

3) You want to carry on looping while i < rhs, not while i >= rhs. You got the logic negated some how

Upvotes: 3

Related Questions