errors when depositing money

Making a bank account program, and it seems to error up when I enter the account number after making a new account and then try to deposit.
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<iostream>
#include<string.h>
#include<process.h>
using namespace std;
class details
{
public:
	char *name;
	int age;
	int accno;
	char branch[50];
	char city[40];
	void getdetails()
	{
		name = new char[20];
		cout << endl << endl << "**********Customer Details*********** " << endl;
		cout << "          -------- -------            " << endl;
		cout << "Enter Name: ";
		cin >> name;
		cout << "Enter Age: ";
		cin >> age;
		cout << "Enter Account Number: ";
		cin >> accno;
		cout << "Enter Branch: ";
		cin >> branch;
		cout << "Enter City: ";
		cin >> city;
		cout << "______________________________________" << endl << endl;
	}
};
class bank
{
public:
	static int accnumber;
	long balance;
	details d;
	void getdata();
	bank transfermoney(bank);
	void deposit();
	void withdrawal();
	void newaccount();
	void viewaccdetails();
};
int bank::accnumber = 0;
int main()
{
	char ch;
	static int i = 0;
	bank *a[10];
	int x, amt, k, j;
	do
	{
		cout << endl << endl << "************MENU************" << endl;
		cout << "            ----            " << endl;
		cout << "1.Create new account\n2.Deposit\n3.Withdraw\n4.Transfer credits\n5.View account details\n\n";
		cout << "Enter choice no.: ";
		cin >> x;
		switch (x)
		{
		case 1:
		{
			i++;
			a[i] = new bank;
			a[i]->newaccount();
			break;
		}
		case 2:
		{
			cout << "Enter account no.: ";
			cin >> k;
			a[k]->deposit();
			break;
		}
		case 3:
		{
			cout << "Enter account no.: ";
			cin >> k;
			a[k]->withdrawal();
			break;
		}
		case 4:
		{
			cout << "Enter the source and destination account nos.: ";
			cin >> k >> j;
			*a[j] = a[k]->transfermoney(*a[j]);
			break;
		}
		case 5:
		{
			cout << "Enter account no.: ";
			cin >> k;
			a[k]->viewaccdetails();
			break;
		}
		}cout << "\nDo you wish to continue[Press 'Y' to continue or 'N' to exit menu]: ";
		cin >> ch;
	} while (ch == 'y' || ch == 'Y');
	system("pause");
	return 0;
}
bank bank::transfermoney(bank a)
{
	long amt;
	cout << "Enter amount to be transferred: ";
	cin >> amt;
	a.balance = a.balance + amt;
	if (balance < amt)
	{
		cout << "\nInsufficient balance! Operation Cannot be performed!" << endl << endl;
	}
	else
	{
		balance = balance - amt;
	}
	return a;
}
void bank::withdrawal()
{
	int amtdrawn;
	cout << "Enter amount to be withdrawn: ";
	cin >> amtdrawn;
	if (balance <= amtdrawn)
		cout << "\nInsufficient balance! Operation Cannot be performed!" << endl << endl;
	else
		balance = balance - amtdrawn;
}
void bank::deposit()
{
	int dep;
	cout << "Enter amount to be deposited: ";
	cin >> dep;
	balance += dep;
}
void bank::newaccount()
{
	accnumber++;
	d.getdetails();
	balance = 0;
}
void bank::viewaccdetails()
{
	cout << endl << endl << "*********ASSIGNMENT BANK ACCOUNT DETAILS*********" << endl;
	cout << "         --- ---- ------- -------         " << endl;
	cout << "Account no.: " << accnumber << endl;
	cout << "Name: " << d.name << endl;
	cout << "Branch: " << d.branch << endl;
	cout << "City: " << d.city << endl;
	cout << "Current Balance: " << balance << endl;
	cout << "_________________________________________" << endl;
}
Last edited on
you didn't happen to do this..
create new account
try to access account #1 (fail! first account is zero!)

did you?
I can't run it, so that was the first thing I noticed... your program is fragile (if you enter bad data it WILL crash).
why though
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<iostream>
#include<string.h>
#include<process.h>
using namespace std;
class details
{
public:
	char *name;
	int age;
	int accno;
	char branch[50];
	char city[40];
	void getdetails()
	{
		name = new char[20];
		cout << endl << endl << "**********Customer Details*********** " << endl;
		cout << "          -------- -------            " << endl;
		cout << "Enter Name: ";
		cin >> name;
		cout << "Enter Age: ";
		cin >> age;
		cout << "Enter Account Number: ";
		cin >> accno;
		cout << "Enter Branch: ";
		cin >> branch;
		cout << "Enter City: ";
		cin >> city;
		cout << "______________________________________" << endl << endl;
	}
};
class bank
{
public:
	static int accnumber;
	long balance;
	details d;
	void getdata();
	bank transfermoney(bank);
	void deposit();
	void withdrawal();
	void newaccount();
	void viewaccdetails();
};
int bank::accnumber = 0;
int main()
{
	char ch;
	static int i = 0;
	bank *a[10];
	int x, amt, k, j;
	do
	{
		cout << endl << endl << "************MENU************" << endl;
		cout << "            ----            " << endl;
		cout << "1.Create new account\n2.Deposit\n3.Withdraw\n4.Transfer credits\n5.View account details\n\n";
		cout << "Enter choice no.: ";
		cin >> x;
		switch (x)
		{
		case 1:
		{
			i++;
			a[i] = new bank;
			a[i]->newaccount();
			break;
		}
		case 2:
		{
			cout << "Enter account no.: ";
			cin >> k;
			a[k]->deposit();
			break;
		}
		case 3:
		{
			cout << "Enter account no.: ";
			cin >> k;
			a[k]->withdrawal();
			break;
		}
		case 4:
		{
			cout << "Enter the source and destination account nos.: ";
			cin >> k >> j;
			*a[j] = a[k]->transfermoney(*a[j]);
			break;
		}
		case 5:
		{
			cout << "Enter account no.: ";
			cin >> k;
			a[k]->viewaccdetails();
			break;
		}
		}cout << "\nDo you wish to continue[Press 'Y' to continue or 'N' to exit menu]: ";
		cin >> ch;
	} while (ch == 'y' || ch == 'Y');
	system("pause");
	return 0;
}
bank bank::transfermoney(bank a)
{
	long amt;
	cout << "Enter amount to be transferred: ";
	cin >> amt;
	a.balance = a.balance + amt;
	if (balance < amt)
	{
		cout << "\nInsufficient balance! Operation Cannot be performed!" << endl << endl;
	}
	else
	{
		balance = balance - amt;
	}
	return a;
}
void bank::withdrawal()
{
	int amtdrawn;
	cout << "Enter amount to be withdrawn: ";
	cin >> amtdrawn;
	if (balance <= amtdrawn)
		cout << "\nInsufficient balance! Operation Cannot be performed!" << endl << endl;
	else
		balance = balance - amtdrawn;
}
void bank::deposit()
{
	double dep;
	cout << "Enter amount to be deposited: ";
	cin >> dep;
	balance += dep;
}
void bank::newaccount()
{
	accnumber++;
	d.getdetails();
	balance = 0;
}
void bank::viewaccdetails()
{
	cout << endl << endl << "*********ASSIGNMENT BANK ACCOUNT DETAILS*********" << endl;
	cout << "         --- ---- ------- -------         " << endl;
	cout << "Account no.: " << accnumber << endl;
	cout << "Name: " << d.name << endl;
	cout << "Branch: " << d.branch << endl;
	cout << "City: " << d.city << endl;
	cout << "Current Balance: " << balance << endl;
	cout << "_________________________________________" << endl;
}






what is wrong with this function, errors up after I make the account and try to deposit
1
2
3
4
5
6
7
8
void bank::deposit()
{
	double dep;
	cout << "Enter amount to be deposited: ";
	cin >> dep;
	balance += dep;   // saying this is the problem
}
void bank::newaccount
why is its out of bounds.
you add an account, its account zero in the array.
if you try to access account 1 or 2, they don't exist, and you hit a bad pointer and it will crash.

what *exact* input are you providing to test your program?

you are adding a double to an integer (dep is double, balance is int). this is not wrong, but it may mean your data is being damaged (loss of decimal places and occasional rounding error).
Last edited on
Topic archived. No new replies allowed.