Creating Bank Program with 2 Accounts

Hi all, I am having trouble trying to create a bank account program. I have I think all the defined headers and have the functions I need. But I am struggling with writting the program to create 2 accounts. 1 account lets say account number 123 will have a balance starting with 10 and then other account 943 will have the default balance which is zero.
How do I create these accounts with define intial balances and then how could I loop through first using defined deposit and then the withdrawal for each account.
Thanks

Here is the header
account.h
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
 #ifndef account_h_
#define account_h_

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class account
{
public:
	//----------------------------------------------------
   	//account
   	account();
   	account(const std::string acctnum);
   	~account();
	//----------------------------------------------------
   	//account number
   	inline const std:string get_acctnum() const{
   		return m_acctnum;
   	}
	//----------------------------------------------------
   	//deposit
   	inline deposit(){
   		deposit(15);
   	}
   	//----------------------------------------------------
   	//withdraw
   	void withdraw(){
   		withdraw(3);
   	}
	//----------------------------------------------------
   	//current balance
   	inline const double get_balance() const{
   		return m_balance;
   	})
	//----------------------------------------------------
   	//display string
   	const std::string asString() const;  	
	
private:
	//----------------------------------------------------
	//account number
	int m_acctnum;
	//----------------------------------------------------
	//balance
	double m_balance;
};

#endif 

Program file
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
//account.cpp

#include <iostream>
#include <string>
#include <stdlib.h>
#include "account.h"
using namespace std;



//----------------------------------------------------
//bank account default balance
account::account(){
	m_balance = 0;
	m_acctnum = "???";
}
account::account(const std::string acctnum){
	m_balance = 0;
	m_acctnum = acctnum;
}
account::~account(){	
}
//----------------------------------------------------
//deposit
void account::deposit(double amount){
	balance = balance + amount;
}
//----------------------------------------------------
//withdraw
void account::withdraw(double amount){
	if(amount < balance ){
		std::cout << "Debit amount exceeded account balance." 
		<< amount << endl;
	}
	else if(amount < 0){
		std::cout <<"The withdrawel you've enter is defined as negative." 
		<< amount << endl;
	}
	else{
		balance = balance - amount;
	}
}
//----------------------------------------------------
//get balance
double account::get_balance() const{
	return balance;
}
//----------------------------------------------------
//display
const std::string account::asstring() const{
	ostringstream oss;

	oss << "Account Num: " << m_acctnum <<
		   " Balance: " << m_balance;
	return oss.str();
}

Problem file
1
2
3
4
5
6
7
8
9
10
11
#include <stdlib.h>
#include <iostream>
#include <string>

#include "account.h"

void main(){
	//define bank accounts
	acct (923, 0);
	acct (124, 100);
}
Last edited on
Use an array (or better yet vector) to store more than one account.
I would create a class Bank that holds a vector of accounts.
How would I do that? And then I would need to loop through. So I would create a ACCT then initiate a balance then loop through applying a deposit and then withdrawal and provide an ending balance. Then I would need to do this again with a new account. Then SUM both accounts.
Last edited on
You have a number of problems with your program.

account.h
------------
Line 25: You're declaring a deposit function that takes no arguments, then calls another deposit function (which doesn't exist) and passes 3. This makes no sense. Your declaration must match the implementation in the .cpp file (line 25).

Line 30: Ditto for Withdraw().

main.cpp
-----------
Line 9-10: This is not the proper way to instantiate you account class. 1) You must name the class. 2) You must give each object a different name (or place the objects in an array or vector as previously suggested. 3) You have no constructor for your class that takes 2 arguments.

1
2
3
 
  account acct1 (923, 0);
  account acct2 (124, 100);

How is this for a revised header
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
#ifndef account_h_
#define account_h_

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class account
{
public:
	//----------------------------------------------------
   	//account
   	account();
   	account(const std::string acctnum);
   	~account();
	//----------------------------------------------------
   	//account number
   	inline const std:string get_acctnum() const{
   		return m_acctnum;
   	}
	//----------------------------------------------------
   	//deposit
   	void deposit(float amt){
   		amt += m_balance;
   	}
   	//----------------------------------------------------
   	//withdraw
   	void withdraw(float amt){
   		amt -= m_balance;
   	}
	//----------------------------------------------------
   	//current balance
   	const double get_balance() const{
   		return m_balance;
   	})
	//----------------------------------------------------
   	//display string
   	const std::string asString() const;  	
	
private:
	//----------------------------------------------------
	//account number
	int m_acctnum;
	//----------------------------------------------------
	//balance
	double m_balance;
};

#endif 
Lines 26: The statement is reversed. It should be:
 
  m_balance += ant;

Line 31: Ditto

Ok so this is what I now have. So looking into arrays. I think I need to perform a multidimensional array to define acctnum and balance. Such as I defined {acctnum=121, balance=""} by default the balance would be 0, and then I want to define another account {acctnum=32, balance=32}. With each of these I then want to perfrom a deposit to an account and a with drawal from an each. I want to do this for both.

header.h
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
ifndef account_h_
#define account_h_

#include <iostream>
#include <string>
#include <account>

using std::account;

class account
{
public:
	account();
	account(const std::string acctnum);
	~account();
	//----------------------------------------------------
   	//account number
   	const std:string get_acctnum() const{
   		return m_acctnum;
   	}
	//----------------------------------------------------
   	//deposit
   	void deposit(float amt){
   		m_balance += amt;
   	}
   	//----------------------------------------------------
   	//withdraw
   	void withdraw(float amt){
   		m_balance -= amt;
   	}
	//----------------------------------------------------
   	//current balance
   	const double get_balance() const{
   		return m_balance;
   	})
	//----------------------------------------------------
   	//display string
   	const std::string asString() const;  	
	
private:
	//----------------------------------------------------
	//account number
	int m_acctnum;
	//----------------------------------------------------
	//balance
	double m_balance;
};

#endif 


program
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
#include <iostream>
#include <string>
#include <stdlib.h>
#include "account.h"
using namespace std;



//----------------------------------------------------
//bank account default balance
account::account(){
	m_balance = 0;
	m_acctnum = "???";
}
account::account(const std::string acctnum){
	m_balance = 0;
	m_acctnum = acctnum;
}
account::~account(){	
}
//----------------------------------------------------
//deposit
void account::deposit(double amount){
	balance = balance + amount;
}
//----------------------------------------------------
//withdraw
void account::withdraw(double amount){
	if(amount < balance ){
		std::cout << "Debit amount exceeded account balance." 
		<< amount << endl;
	}
	else if(amount < 0){
		std::cout <<"The withdrawel you've enter is defined as negative." 
		<< amount << endl;
	}
	else{
		balance = balance - amount;
	}
}
//----------------------------------------------------
//get balance
double account::get_balance() const{
	return balance;
}
//----------------------------------------------------
//display
const std::string account::asstring() const{
	ostringstream oss;

	oss << "Account Num: " << m_acctnum <<
		   " Balance: " << m_balance;
	return oss.str();
}


test
*So this where I am lost. So I have the 2 accounts. Now I need to take the first account with deposit and perform a withdrawal of some amount and then a deposit of some amount. Then go to second deposit some amount and then deposit some amount. Then display intial deposit, then after withdrawal and after second deposit and what is account is after these actions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

#include "account.h"



void main(){
	account::account[][2] = {
		{44421, 20},
		{55531, }
	};

	for (int row = 0; row < 2; ++row)
	{
		for (int col = 0; col < 2 ; ++col)
		{
			account::deposit[col]
		}
	}

}
Last edited on
So, first: The type of m_acctnum is int. Do not pass a string.

That's not the way arrays work. You don't need a 2-dimensional array. What you need is a 1-dimensional array where you can access the 2 member variables:
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
#include <iostream>

class account
{
...
	//----------------------------------------------------
   	//deposit
   	void deposit(float amt){
   		m_balance += amt;
   	}
...	
private:
	//----------------------------------------------------
	//account number
	int m_acctnum;
	//----------------------------------------------------
	//balance
	double m_balance;
};


int main(){
	account a_array[] = { // Note: a_array
		{44421, 20},
		{55531, 30}
	};

	for (int i = 0; i < 2; ++i)
	{
		a_array[i].deposit(100); // Note: a_array[i].
	}

    return 0;
}


So if you implement a constructor and you want to initialize it like above you need at least one constructor that takes two parameter [for initializing both member variable m_balance and m_acctnum]:
1
2
3
4
5
6
7
8
9
10
class account
{
...
  account(int acctnum, double balance)
  {
    m_acctnum = acctnum;
    m_balance = balance;
  }
...
};
or better yet (using initializer list):
1
2
3
4
5
6
7
8
9
10
class account
{
...
  account(int acctnum, double balance) : 
    m_acctnum(acctnum)
    , m_balance(balance)
  {
  }
..
};
Last edited on
Perfect thanks guys I got this working!
Topic archived. No new replies allowed.