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.
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:
Where "RATE" is defined above the class as constexprdouble 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.
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;
}