Travis Waelbroeck
Travis Waelbroeck

Reputation: 2135

Intro Bank Account Class - Strange Output

This is for an intro to classes assignment in which I am to create a bank account with the ability to manipulate a balance by depositing or withdrawing money and to look up the balance of the account.

I've looked over a dozen different examples and how-to's but now I'm at a loss.

The only output I get from my code is Account Balance: $-8589937190 I have no idea where this value is coming from. Any ideas on where I should be starting?

#include <iostream>
using namespace std;

// Define Account class
class Account
{
public:
    Account(int startingBal = 0){
        m_startingBal = startingBal;
}
void credit(int amount);
void withdraw(int amount);
int getBalance() const;
private:
int m_startingBal;
int balance;
};
void Account::credit(int amount)    // deposit money
{
balance += amount;
};
void Account::withdraw(int amount)  // withdraw money
{
balance -= amount;
};
int Account::getBalance() const     // return the current balance
{
cout << "Account Balance: $" << balance << endl;
return balance;
};
int main()
{
Account account(1500); // create an Account object named account with startingBal of $1500
account.credit(500);    // deposit $500 into account
account.withdraw(750);  // withdraw $750 from account
account.getBalance();   // display balance of account

system("PAUSE");    // to stop command prompt from closing automatically
return 0;
} // end main

Upvotes: 0

Views: 1253

Answers (1)

Borgleader
Borgleader

Reputation: 15916

The balance member variable is never assigned to (in the constructor) and therefore contains a garbage value.

In fact you seem to have a bug. In your constructor you set m_startingBal but don't use it anywhere else, while balance is not set in the constructor but is used everywhere else.

Upvotes: 3

Related Questions