+ 1

What am i doing wrong ?

I'm working on a task in C++ , Bank Account Balance , and i don't understand what I'm doing wrong. #include <iostream> using namespace std; int main() { int balance; int withdraw; cin >> balance; cin >> withdraw; // your code goes here return 0; } I've tried adding another integer ; "int x = balance -= withdraw; int main() { int balance; int withdraw; int x = balance -= withdraw; cin >> balance; cin >> withdraw; // ваш код cout << x << endl; return 0; } I've rewrote cout several times , used "-" instead of "-=" in int x. Nothing has succeeded so far. Any and all input will be greatly appreciated. EDIT : Shadow is correct ! It worked ! Thank you so much ! :D. Have a great & wonderful day , everyone ! :).

21st Jan 2021, 2:11 PM
Kirill Chernyatin
Kirill Chernyatin - avatar
2 ответов
+ 5
Your program is executed from top to bottom. That means when doing calculations, it is crucial to have the necessary values stored before operating on them. If you subtract the withdrawn amount before having the user enter how much money to withdraw, what do you expect to happen? I don't know the exact task, but if you are supposed to print the difference between current balance and the withdrawn amount, it should be sufficient to write cout << balance - withdraw << endl; after acquiring the input.
21st Jan 2021, 2:23 PM
Shadow
Shadow - avatar
0
Holy crap. It worked ! Thank you so much ! :D. Have a great & wonderful day ! :).
21st Jan 2021, 2:25 PM
Kirill Chernyatin
Kirill Chernyatin - avatar