I can't transfer the money and ı want to substract the money from sender how can ı do that?

Write your question here.

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
  #include<iostream>
#include<string>

using namespace std;

class Account
{
public:
	

	Account(string nm, long int acno, double bala)
	{
		setBalance(bala);
	}
	double deposit(double para)
	{
		balance += para;
		return balance;
	}
	double withDraw(double takeDraw)
	{
		balance -= takeDraw;
		return balance;

	}
	double addInterest()
	{
		balance = balance*rate;
		return balance;
	}

	double getBalance()
	{
		return balance;

	}

	void setBalance(double blnc)
	{
		balance = blnc;

	}

	double moneyTransfer(Account x ,double transfer)
	{
		double money;
		money = x.deposit(transfer);
		x.setBalance(money);//where is the problem ???

		
		
		return x.balance;
	}
private:
	double rate = 0.025;
	long int accountNumber;
	double balance;
	string name;


};

int main()
{
	double transfer1;
	

	Account	 user1("FURKAN", 2018, 1000);
	Account user2("MURAT", 2017,  0);
	Account user3("MELTEM", 2016, 150);

	cout << "Please enter the amount of money that you want to transfer = ";
	cin >> transfer1;
	
	cout << user2.moneyTransfer(user2, transfer1) << endl;
	

	cout << user2.getBalance() << endl;
	cout << user1.getBalance() << endl;
	

	

	system("pause");

	return 0;
}
Hello FURKANkartal,

It looks like the problem starts at line 75. You are using the account of "user2" to withdraw money from, but the parameter says that you want to send the money to "user2". I think here you meant to use "user1" or "user3".

Inside your function starting at line 44 I am thinking that first you would withdraw from the sending account before you add to the receiving account.

My observation without testing.

Hope that helps,

Andy
Hello FURKANkartal,

When I ran the program I found that you created an overloaded ctor taking three parameters, but you only use one to call "setBalance(bala);" The ctor is used to create an object of the class, so it already has access to the private variables. Your ctor should look more like this:

1
2
3
4
5
6
7
8
Account(string nm, long int acno, double bala)
{
	rate = RATE;
	accountNumber = acno;
	balance = bala;
	name = nm;
	//setBalance(bala);
}

Where "RATE" is defined above the class as constexpr double RATE{ 0.025 };. If you had put the class in a header file and the member functions in a ".cpp" file I would put the definition of "RATE" at the beginning of the ".cpp" file and make a note in main where the rate needs changed. "rate" should be set in the ctor not in the private section of the class. If rate should change for different users then I would add a forth parameter to the overloaded ctor.

As I thought the "moneyTransfer" function is using the same account for the sender and receiver.

In your original set up account 2 has a zero balance. You need to check for this before sender can send any money. Otherwise the sender could end up with a negative balance.

This is what I have found so far.

Hope that helps,

Andy
Hello FURKANkartal,

The last thing I found is with the double moneyTransfer(Account x, double transfer function.

As is the account being passed to the function is being passed by value, i.e., a copy. Everything works well until the function and the changes to the copy are lost.

Some day I can be thick. It took me several runs before I realizes that the account needs to be passed by reference: double moneyTransfer(Account& x, double transfer.

I believe this code for this function is more of what you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double moneyTransfer(Account& x, double transfer)
{
	if (balance < transfer)
	{
		std::cout << "\n Not enough money to cover transfer amount." << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
		return 0.0;
	}

	balance -= transfer;
	x.deposit(transfer);  // <--- Notice deposit and not set as you had. You need to add to balance not set.

	//money = x.deposit(transfer);
	//x.setBalance(money);//where is the problem ???

	return x.balance;
}


Hope that helps,

Andy
Thank you guys .I solve the problem
Andy you're the best thank you
Topic archived. No new replies allowed.