Help Debugging my Program! Using Functions

So basically I am writing a program that is used to return the current amount in a bank account. The amount in the account defaults to zero. The user inputs whether it is a deposit or withdrawl, and also how much they are depositing or withdrawing. It works when I am using a deposit, but when I denote I am with drawing money it doesn't return a negative number like I am wanting it to. What do I need to change?



#include <iostream>
#include <string>

using namespace std;

//
int update_balance (int& dollars, int& balance, string& command)
{
if (command=="deposit")
{
balance = balance + dollars;
}
else if (command=="withdraw")
{
balance = balance - dollars;
}
return balance;
}
//

int main()
{
//the amount of money in your account
int balance = 0;

// Command that will tell your function what to do
string command;
cin >> command;

// number of dollars you would like to deposit or withdraw
int dollars = 0;
cin >> dollars;

balance = update_balance(balance, dollars, command);

// Prints out the balance
cout << balance << endl;

return 0;
}
The problem is that you pass dollars and balance swapped:

1
2
3
int update_balance (int& dollars, int& balance, string& command)
...
balance = update_balance(balance, dollars, command);
Topic archived. No new replies allowed.