I need Help Please!!!

Hello everyone,i have just recently started studying c++, i was trying to create a simple banking program that prompts the user to enter his/her name,account number and account type. For now i have set the program to accept details for two accounts. After detail input the user selects an account to work with by entering the corresponding account number then he/she can choose 1 of four operations to perform which are,check balance,deposit,withdraw and transfer. The functions for the deposit, check balance and withdraw i have defined and they work fine.
I am having a problem with my transfer function.
When the user selects the transfer option he/she will be prompted to enter account to transfer the money to,then to enter amount to be transferred. the function should be able to deduct from sending account and add to receiving account then display the current balance for both accounts. I only got the deducting part and the display of balance of the sending account to work. Please may anyone be of assistance because i have tried everything i know and have failed to make it work.
The transfer function is from line 58 to 81.

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
  #include <iostream>
#include <string>
using namespace std;

class account{
	private:
	string Dname, AccountType, AccountNum;
	float balance;
	public:
		void setdata(string, string, string);
		void setbalance(float);
		void deposit(float);
		void withdraw(float);
		void initializer(void);
		void displaydata(void);
		float getbalance(void);
		string getaccnum(void);
		friend void tranfer(account,string,float);
};

void account::setdata(string a, string b, string c){
	Dname = a;
	if(b != ""){
		AccountType = b;
	}
	AccountNum = c;	
}
void account::deposit(float x){
	balance = balance + x;
}
void account::withdraw(float z){
	if(z >= balance){
		cout<<"Insufficient Funds.\n";
	}
	else{
		balance = balance - z;
	}	
}
void account::initializer(void){
	balance = 300000.00;
	AccountType = "Savings";
}
void account::displaydata(void){
	cout<< "Account Holder: " << Dname << endl;
	cout<< "Account Type: " << AccountType << endl;
	cout<< "Account Numebr: " << AccountNum << endl;
	cout<< "Account Balance: " << balance << endl;
}
float account::getbalance(void){
	return balance;
}
string account::getaccnum(void){
	return AccountNum;
}
void account::setbalance(float y){
	balance = y;
}
void tranfer(account object, string temp2, float amount){
{
	float tempo;
	if(object.getbalance() <= amount){
		cout << "Sorry Transaction not possible,Insufficient Funds";
	}
	else{tempo = object.getbalance();
		tempo = tempo - amount;
		object.setbalance(tempo);
	}
}
{
	float tempo2;
	account object2[2];
	for(int j = 0; j <= 1; ++j){
		if(object2[j].getaccnum() == temp2){
			tempo2 = object2[j].getbalance();
			tempo2 = tempo2 + amount;
			object2[j].setbalance(tempo2);
			cout << "New Balance for Account " << temp2;
		}	
}
cout << "New Balance for Account " << object.getaccnum() << " is: " << object.getbalance();
}

int main (){

	account object[2];
	string a,b,c,temp,temp2;
	float x,z,amount;
	int choice,endelea;
	for(int i = 0; i<=1; ++i)
	{
	object[i].initializer();
	cout << "Please Enter Depositor's name: ";
	getline(cin,a);
	cout << "Please Enter Account Type: ";
	getline(cin,b);
	cout << "Please Enter Account Number: ";
	getline(cin,c);
	object[i].setdata(a,b,c);
	cout << endl;
	}
	for(int i = 0; i <=1; ++i){
		object[i].displaydata();
		cout << "******************************************************\n";
	}
	
	cout << "Please choose Account by Entering Account Number: ";
	getline(cin,temp);	
	cout << "Enter Number to select Transaction \n" << "1: Check Balance \n" << "2: Withdraw \n" << "3: Deposit\n" << "4: Transfer\n";
	cin >> choice;
	for(int i=0; i<=1; ++i){
	
		if(choice == 1 && object[i].getaccnum() == temp){
			cout << "Your Balance is: " << object[i].getbalance();
		}
		else if(choice == 2 && object[i].getaccnum() == temp){
			cout << "Please Enter amount to withdraw: ";
			cin >> z;
			object[i].withdraw(z);
			object[i].displaydata();
		}
		else if(choice == 3 && object[i].getaccnum() == temp){
			cout << "Please Enter Amount to Deposit: ";
			cin >> x;
			object[i].deposit(x);
			object[i].displaydata();
		}
		else if(choice == 4 && object[i].getaccnum() == temp){
			cout << "Please Enter Account Number of Account to Transfer to: ";
			cin >> temp2;
			cout << "Please Enter Amount to Transfer: ";
			cin >> amount;
			tranfer(object[i], temp2, amount);
		}
			
	}
return 0;
}.
closed account (j3Rz8vqX)
Obviously, there are incorrect numbers of curly braces in your transfer function; but I suppose that may not be the solution you are asking for:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void tranfer(account object, string temp2, float amount)//Possibly pass your array of objects[2] from main?
{
	if(object.getbalance() <= amount){
		cout << "Sorry Transaction not possible,Insufficient Funds";
	}
	else{
		object.setbalance(object.getbalance() - amount);
	}
	account object2[2];//Empty accounts? this was constructed here! Maybe pass your array of objects from main?
	for(int j = 0; j <= 1; ++j){
		if(object2[j].getaccnum() == temp2){//Will never be true! unless they're both NULL, because object2[2] has not be initialized.
			object2[j].setbalance(object2[j].getbalance() + amount);//Adding amount to balance? 0+amount?
			cout << "New Balance for Account " << temp2;
		}
    }
    cout << "New Balance for Account " << object.getaccnum() << " is: " << object.getbalance();
}

I have not solved your problem, but the comments may lead to doing so.

My assumption: you will want to pass your array of objects and possibly pass the single object by reference '&'.
Last edited on
thank you for the hints,i will try to work on it and give you some feedback.
Topic archived. No new replies allowed.