Banking system

So, this is going to get a little complex. I've slowly been building up my banking system and need help. First though a quick run through. 1. choose an existing account or new one. 2. after that it will display the options of withdraw, deposit or close account (close account is the least of my worries as of right now). I've been using two different text documents as well. One is "Accounts" where it will scan or add accounts the other is "Money" where it should keep track of money in each account. The question is how can I assign the money to each account? For example, when I choose the account named "John Doe" with a balance of $45.00 and wish to withdraw $20.00 I want the money for John Doe to decrease to $25.00 and NOT make a new line in the "Money" text document saying $20.00.


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
#include <iostream>
#include <fstream>
#include <iomanip>//used to move text 
#include <string>
using namespace std;

int display(char& anwser);
void N_account(char& anwser,string & name);
int Exist(string& name_search, char& anwser, string name_from_file, char& anwser2,string & money_deposit);
void deposit(string& money_deposit, char& anwser, char& anwser2,int & money_D);
void withdraw(string& withdraw_money, char& anwser, char& anwser2, int& money_D, string& total_money, int& money_W);


int main()
{
	int start_money, money_D, money_W;
	string name, name_search, name_from_file, money_deposit, withdraw_money, total_money;
	char anwser, anwser2;
	display(anwser);
	if (anwser == '1')
	{
		N_account(anwser, name);
	}
	if (anwser == '2')
	{
		Exist(name_search, anwser, name_from_file, anwser2, money_deposit);
	}
	if (anwser2 == '1')
	{
		deposit(money_deposit, anwser, anwser2, money_D);
	}
	if (anwser2 == '2')
	{
		withdraw(withdraw_money, anwser, anwser2, money_D, total_money, money_W);
	}
	
}

int display(char& anwser)
{
	
	cout << setw(65) << "=================" << endl;
	cout << setw(65) << "Banking Managment" << endl;
	cout << setw(65) << "=================" << endl;
	cout << setw(60) << "1.New account" << endl;
	cout << setw(65) << "2.Existing account" << endl;
	cin >> anwser;
	return 0;
}

void N_account(char& anwser,string & name)
{
	
			ofstream outfile;
			outfile.open("Accounts.txt",std::ofstream::out|std::ofstream::app);
			cout << "Enter in first and last name for new account:";
			cin.ignore();
			getline(cin, name);
			outfile << name;
			outfile << endl;
			cout << "Account added" << endl;
			outfile.close();

	
}

int Exist(string & name_search,char & anwser,string name_from_file,char & anwser2,string &money_deposit)
{
		ifstream infile;
		infile.open("Accounts.txt");
		cout << "Enter in your account:";
		cin.ignore();
		getline(cin, name_search);
		while (getline(infile,name_from_file))//This scans the Accounts file and looks for the whole name of an existing account
		{
			if (name_from_file == name_search)
			{
				cout << "Account found: " << name_search << endl;
				cout << "Choose what you would like to do" << endl;
				cout << setw(56) << "1.Deposit" << endl;
				cout << setw(57) << "2.Withdraw" << endl;
				cout << setw(62) << "3.Close account" << endl;
				cin >> anwser2;
				return 0;
			}
			
		}
		infile.close();
	
}

void deposit(string& money_deposit, char& anwser,char & anwser2,int & money_D)
{
	
		ofstream O_file;
		O_file.open("Money.txt", std::ofstream::out | std::ofstream::app);
		cout << "Enter in how much you would like to deposit: ";
		cin.ignore();
		getline(cin, money_deposit);
		money_D = stoi(money_deposit);//converts string into integer so that I may use basic operators such as less than or subtraction ect.
		if (money_D < 0)
		{
			cout << "Error!" << endl;
			system("pause");
			exit(1);
		}
		O_file << money_deposit;
		O_file << endl;
		O_file.close();
	
}

void withdraw(string& withdraw_money, char& anwser,char & anwser2,int & money_D,string &total_money,int & money_W)
{
	
		ofstream O_file;
		O_file.open("Money.txt", std::ofstream::out | std::ofstream::app);
		cout << "Enter in how much you would like to withdraw:";
		cin.ignore();
		getline(cin, withdraw_money);
		money_W = stoi(withdraw_money);//converting string to integer
		total_money = money_W - money_D;
		O_file << total_money;
		O_file << endl;
		O_file.close();
	
}

BTW the existing accounts are
1
2
3
4
5
6
John Doe
Jane Doe
Travis Scott
William Smith
Patrick Michaels
Courtney Desmond


And the money for each of them is
1
2
3
4
5
6
45.00
98.00
48.00
56.00
120.00
You can use an array that'll hold the amount of money each person has. Every time you withdraw, you simply subtract it.

Assuming the format of the file you're reading is like this:
John Doe 45.00


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct people
{
	std::string name;
	double money;
};


int main()
{
	people m[6]; //We have 6 people

	std::ifstream infile;
	infile.open("Accounts.txt");

	for(int i = 0; i < 6; i++) //Have The Struct Array Hold Everything
	{
		infile >> m[i].name >> m[i].money;
	}
}


Now, you've taken all the data off your file and no longer are dependent on it for the information. From here, whenever they withdraw money, your function will simply have to:

1
2
3
4
5
6
7
8
9
10
11
12
13
void withdraw(people m[], std::string name, double amount)
{
	int i = 0;
	for (; i < 6; i++) //Look for the person in the array
	{
		if(m[i].name == name)
		{
			break;
		}
	}

	m[i].money -= amount; //Subtract the amount they've withdrawn
}
ok but I have a problem now with calling the withdraw function from the main function

1
2
3
4
if (anwser2 == '2')
	{
		withdraw(m[], name, amount);//the brackets for the m are causing a crash
	}
Don't add the brackets, you only need to give it the variable name. The brackets are only there in the parameters for the function itself. When calling it, it'll just be plain "m"
Last edited on
I removed the brackets only for "withdraw" to be unlined with a red line.
Did you change your forward declaration of the function in line 11 to:

void withdraw(people m[], std::string name, double amount); ?

withdraw(m, name, amount); Is correct
Last edited on
Yea I did, but for some reason it's not working. That's weird.
Here's the main function where the problem is.

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
int main()
{
	people accounts[6];//6 people held in here
	ifstream infile;
	infile.open("Accounts.txt");

	for (int i = 0; i < 6; i++) //Have The Struct Array Hold Everything
	{
		infile >> accounts[i].name >> accounts[i].money;
	}

	int start_money, money_D, money_W;
	string name, name_search, name_from_file, money_deposit, withdraw_money, total_money;
	char anwser, anwser2;
	double amount;
	display(anwser);
	if (anwser == '1')
	{
		N_account(anwser, name);
	}
	if (anwser == '2')
	{
		Exist(name_search, anwser, name_from_file, anwser2, money_deposit);
	}
	if (anwser2 == '1')
	{
		deposit(money_deposit, anwser, anwser2, money_D);
	}
	if (anwser2 == '2')
	{
		withdraw(accounts, name, amount);
	}
	
}
Make sure the function and its declaration are BELOW the struct:

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
struct people
{
	std::string name;
	double money;
};

void withdraw(people m[], std::string name, double amount);

int main()
{
     //stuff
}

void withdraw(people m[], std::string name, double amount)
{
	int i = 0;
	for (; i < 6; i++)
	{
		if (m[i].name == name)
		{
			break;
		}
	}

	m[i].money -= amount;
}


However, you wanna do it, make sure the struct is above anything that uses it.
Last edited on
Topic archived. No new replies allowed.