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?

20th Feb 2021, 11:04 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
26 Respostas
+ 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; _______________________
20th Feb 2021, 11:09 AM
noteve
noteve - avatar
+ 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.
20th Feb 2021, 12:03 PM
Ipang
+ 4
Ipang Angelo Arsenic I didn't know that placing parentheses around it would also work. Thanks for the new knowledge/info.
20th Feb 2021, 12:36 PM
noteve
noteve - avatar
+ 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
20th Feb 2021, 12:33 PM
Arsenic
Arsenic - avatar
+ 2
Because it reads (cout << balance) -= withdraw While you want cout << (balance -= withdraw)
20th Feb 2021, 11:53 AM
Angelo
Angelo - avatar
+ 2
Ipang what you're saying is partially true (for example) in python but not in c/c++ There is practicaly no statement/expression in c/c++, a=2 is treated the same as a+2
20th Feb 2021, 12:08 PM
Angelo
Angelo - avatar
+ 2
Eve assignment operator returns the value of it's left operand after the assignment.
20th Feb 2021, 12:39 PM
Arsenic
Arsenic - avatar
+ 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.
20th Feb 2021, 12:53 PM
Ipang
+ 1
Thank you, Eve !
20th Feb 2021, 11:22 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 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
20th Feb 2021, 1:00 PM
Angelo
Angelo - avatar
+ 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.
21st Feb 2021, 2:12 AM
Arsenic
Arsenic - avatar
+ 1
šŸ¤¦ā€ā™‚ļø Tahiti , what do you think your program is doing ?
21st Feb 2021, 5:27 AM
Arsenic
Arsenic - avatar
+ 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)
22nd Feb 2021, 7:41 AM
Mir Abir Hossain
Mir Abir Hossain - avatar
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.
20th Feb 2021, 11:12 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
Arsenic Your code gave output ā€˜0ā€™. But we need cout << balance;
20th Feb 2021, 6:03 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
Arsenic cin >> balance; cin >> withdraw;
21st Feb 2021, 2:18 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
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 ?
21st Feb 2021, 3:04 AM
Arsenic
Arsenic - avatar
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 -= ā€˜
21st Feb 2021, 3:28 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
Arsenic Iā€™m not sure what youā€™re asking. The issue has been resolved, but the problem was with ā€œcout 怋balance -= withdraw; ā€œ
21st Feb 2021, 10:12 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
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!
23rd Feb 2021, 4:24 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar