0
Assignment & Increment Operators (Bank Account Balance) C++
I have to output the bank balance after a withdrawal. int balance ; int withdraw ; cin >> balance; cin >> withdraw; cout << balance-= withdraw; //What’s wrong with my answer? //No match for ‘operator -= ‘ //What does this error message mean?
26 Réponses
+ 6
Try to print balance after you have subtracted withdrawal to it or just remove the equal sign.
_______________________
int balance, withdrawal;
cin >> balance;
cin >> withdrawal;
balance -= withdrawal;
cout << balance;
_______________________
---OR---
_______________________
int balance, withdrawal;
cin >> balance;
cin >> withdrawal;
cout << balance - withdrawal;
_______________________
+ 4
*** DELETED ANSWER (WRONG CONCEPT COMPREHENSION) ***
(EDIT)
It turned out that right to left associativity of -= operator affects how the output is inserted to the output stream. This behaviour is changed by addition of parentheses to change priority of evaluation.
(CORRECTION)
Upon recent search I found that assignment and input/output operation in C++ are EXPRESSION and not STATEMENT. So I deleted my previous answer where I stated something wrong.
+ 3
The problem here (as already pointer out by Angelo ) is that compund assignment operator " -= " is right-left associative
Means your expression will be grouped from right to left, making it
(cout<< balance) -= (withdraw)
Simple fix is to override this behaviour by putting parentheses to show how the operands should be grouped.
cout<< (balance -= withdraw)
here is the fixed code 👇
https://code.sololearn.com/czh7JQvTd0HU/?ref=app
+ 2
Because it reads
(cout << balance) -= withdraw
While you want
cout << (balance -= withdraw)
+ 2
Eve assignment operator returns the value of it's left operand after the assignment.
+ 2
Angelo
a = 2 is not the same with a + 2, one thing to consider before placing those two in equal rank is that a + 2 in itself doesn't do anything to <a> unless the result is assigned to something.
And don't forget that a = 2 will be different to a + 2 when the declaration of <a> didn't initialize <a> to a certain value.
+ 1
Thank you, Eve !
+ 1
Ipang I know, I just took the first two operations that came in my mind
What I ment was that they are both functions, regardless of what they do
+ 1
Tahiti what was the input you gave to get the faulty output ?
In any case, the code will print the final value of balance to the screen.
+ 1
🤦♂️
Tahiti , what do you think your program is doing ?
+ 1
I want to add here something, in c++ you can write it
cout << (balance-=withdrawal);
and it will work but in python it won't work in anyway. You have to write separately. Like --
balance -= withdrawal
print(balance)
0
You want to withdraw some money from your bank account.
The program takes two numbers as input, your account balance and the amount you want to withdraw. Calculate and output the remaining balance after the withdrawal.
Use the -= shorthand for easier calculation.
0
Arsenic
Your code gave output ‘0’.
But we need cout << balance;
0
Arsenic
cin >> balance;
cin >> withdraw;
0
Tahiti
That's correct
But you are just telling program to read user's input here. The user still have to give some input.
what was the input you gave at runtime to test this pice of code that gave you wrong output ?
0
Arsenic
//Here is exactly what I entered as code:
int balance ;
int withdraw ;
cin >> balance;
cin >> withdraw;
cout << balance-= withdraw;
// Error message: No match for ‘operator -= ‘
0
Arsenic
I’m not sure what you’re asking. The issue has been resolved, but the problem was with “cout 》balance -= withdraw; “
0
Pavan Chaitanya
Could you maybe create a new post for your question, include a link to the exercise, and also indicate which programming language you are using? Thank you, and good luck!