-2

The code below compiles properly, but the calculation seems to be an issue. When printing out the first deduction 'withdraw', the customerBalance is 2.07361e (8032.78 - 244.0 = 7788.78)? The data members are private, but it didn't appear to be an issue with it being public or private. I'm not sure. Any suggestions would be welcome. Thank you.

#include <iostream>
#include "BankAccount.hpp"
using namespace std;

BankAccount::BankAccount(string name, string ID, double balance)
{
    customerName = name;
    customerID = ID;
    customerBalance = balance;

}

string BankAccount::getCustomerName()
{
    return customerName;
}

string BankAccount::getCustomerID()
{
    return customerID;
}

double BankAccount::getCustomerBalance()
{
    return customerBalance;
}

void BankAccount::withdraw(double w)
{

    customerBalance = (customerBalance - w);
}

void BankAccount::deposit(double d)
{
    customerBalance = (customerBalance + d);
}

int main()
{
    double customerBalance;

    BankAccount account1("Harry Potter", "K4637", 8032.78);
    account1.withdraw(244.0);
    cout << customerBalance;
    account1.withdraw(3012.58);
    account1.deposit(37.54);
    account1.withdraw(1807.12);
    account1.deposit(500.00);
    double finalBalance = account1.getCustomerBalance();
 }
ajt
  • 1
  • 1
  • 3
    The customerBalance that you declare in main() is not the same as the one in the class. You're basically printing an unasigned variable. – DeiDei Apr 28 '16 at 01:45
  • Also, seriously, stop using `double` for currency. It's a completely unsuitable type. http://stackoverflow.com/a/3730040/327083 , http://stackoverflow.com/q/273371/327083 , http://stackoverflow.com/q/32212213/327083 , etc... – J... Apr 28 '16 at 02:04

1 Answers1

3

The cusotmerBalance you've declared in main is different than BankAccount::customerBalance. You never initialize customerBalance, so it has an undefined value when you print it.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52