Banking system question

I'm almost done with my banking system, but I'm trying to figure out how to set up the total_money formula correctly for depositing and withdrawing to keep track on how much money is in the account.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include <iostream>
using namespace std;

class SavingsAccount
{
public:
	SavingsAccount();
	SavingsAccount(int d, int c);
	void deposit(int d, int c);
	void withdraw(int d, int c);
	void total(int dollars, int cents);
	void set(int dollars, int cents);
	void convert(int d, int c);
private:
	double total_money;
	int dollars;
	int cents;
	int D_balance;
};

SavingsAccount::SavingsAccount()
{
	dollars = 0;
	cents = 0;
}
void SavingsAccount::set(int dollars, int cents)
{

}

int main()
{
	SavingsAccount bank1, bank2(50, 88);
	int dollars, cents;
	bank1.set(30, 65);//setting bank1 to have initial money as I call upon the void set function above
	bank2.set(0, 0);
	int anwser;
	cout << "Would you like to 1.Deposit or 2.Withdraw?";
	cin >> anwser;
	if (anwser == 1)
	{
		cout << "Enter in how much you want to deposit in dollars:";
		cin >> dollars;
		cout << "Enter in how much you want to deposit in cents:";
		cin >> cents;
		if (dollars < 0 || cents < 0)
		{
			cerr << "Invalid!" << endl;
			exit(1);
		}
		bank1.deposit(dollars, cents);//should call upon the seprate deposit funtction below
	}
	else if (anwser == 2)
	{
		cout << "Enter in how much you want to withdraw in dollars:";
		cin >> dollars;
		cout << "Enter in how much you want to withdraw in cents:";
		cin >> cents;
		if (dollars < 0 || cents < 0)
		{
			cerr << "Invalid!" << endl;
			exit(1);
		}
		bank1.withdraw(dollars, cents);
	}

}
SavingsAccount::SavingsAccount(int d, int c)
{
	dollars = d;
	cents = c;

}
void SavingsAccount::convert(int d, int c)
{
	
	dollars = dollars + c / 100;
	cents = c % 100;//% used for the remainer
}


void SavingsAccount::deposit(int d, int c)
{
	dollars += d;//d is where the program keeps track of the values
	cents += c;
	if (cents >= 100)
	{
		convert(d, c);
	}
	//total_money=???
	total(dollars, cents);
}

void SavingsAccount::withdraw(int d, int c)
{
	dollars -= d;
	cents -= c;
	if (cents >= 100)
	{
		convert(c, d);
	}

	total(dollars, cents);
}

void SavingsAccount::total(int dollars, int cents)
{
	cout << "Total money: " << total_money << endl;

}
Do not store/calculate money as float.

For just displaying a value, you can use the <iomanip>.
See http://www.cplusplus.com/reference/iomanip/setfill/

1
2
3
4
5
6
#include <iostream>     // std::cout, std::endl
#include <iomanip>      // std::setfill, std::setw

int main () {
  std::cout << '$' << 2 << '.' << std::setfill ('0') << std::setw(2) << 5 << '\n';
}

$2.05
Ok the display works just fine now, but what about actually storing the total_money?
You already have all your money in dollars and cents. Why would you have a redundant variable that you would have to update whenever your balance changes?

If you absolutely have to give one number out, then return 100*dollars+cents;
Hello Deadweight77,

As keskiverto pointed out and these days "double" is the preferred floating point type. But even with a "double" there are problems with its use. Not all decimal numbers are stored as entered. When I was testing it out "0.03" actually stored as "0.029999999999999999". This may not seem like a problem and it is not when you just output the number to two decimal places because it will round up to "0.03". But in many calculations this will likely give you a result that is off by a penny or two.

It has been about a year or more, but I did read in a post that in the real world when working with money neither a "float" or "double" is used, but an "int". I may not have the complete understanding of this, but what I have done has worked for me so far.

The variable is based on one(1) equals one(1) penny, so 100 would be one dollar and $1.25 would be 125. Since you do not use a decimal number there is no problem. I will show you later how to pring it out.

You have a good start with you input in main. You just need to follow through with it.

In the class I would consider doing this, although I have not changed your code yet, I feel it is a start:
1
2
3
4
private:
	
	int m_balance;
};

My initial check did not find where "D_balance" was ever used. You may have had an idea for future use of this variable. In that case you can keep it. The "int dollars" and int cents" are not really needed. And as I have seen many times the "m_" prefix is often for class member variables and it does have its advantage.

The function "deposit" can be shortened to:
1
2
3
4
5
6
7
void SavingsAccount::deposit(int d, int c)
{
	m_balance += (d + c);

	//total_money=???
	total();
}

I have not worked on the "withdraw" function yet, but it would be similar.

The function call to "total" has no parameters because you will be using "m_balance" from the class.

"total" which I might consider calling "printBalance" the "cout" statement would be something like this:
std::cout << "\n Your balance is: $" << m_balance / 100 << '.' << m_balance % 100 << '\n" << std::endl;

I do believe that this should work, but I still need to make the changes to your code and test it.

I feel that I may have missed something, so if you have any questions about this let me know.

Hope that helps,

Andy
Hello Deadweight77,

I knew I forgot to mention something.

In "main" when you input your "dollars" and "cents" This may work out better than the if statement that you have.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if (anwser == 1)
{
	std::cout << "\n Enter in how much you want to deposit in dollars: ";
	std::cin >> dollars;

	while (!std::cin || dollars < 0)
	{
		if (!std::cin)
		{
			std::cout << "\n    Invalid input. Must be a number.\n";
			
			std::cin.clear();
			std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

		}
		else if (dollars < 0)
		{
			std::cout << "\n    Invalid amount. Must be greater than 0.\n";
		}

		std::cout << "\n Enter in how much you want to deposit in dollars: ";
		std::cin >> dollars;
	}

Since you are using formatted input the "cin" expects a number to be entered and if it is not then "cin" will be in a failed state that needs corrected or it will be unusable the rest of the program. The advantage of the while loop is that you will stay in the while loop until you have entered a valid number. There is no need to exit the program and start over.

The same concept can be used after you enter the "cents".

You should find that this while loop works out better than the ifn statement that you have.

Hope that helps,

Andy
Hello Deadweight77,

I apologize for this, but I do not use this type of code for working with money that often, so regurgitating the proper code did not come out correctly.

Lines of m_balance += (d + c); should be written as m_balance += ((dc * 100) + c);.

The () are not really needed as the multiplication is done before the addition either way, but they do show intent of what you are doing.

Andy
Topic archived. No new replies allowed.