void closeAcct(Bank &bank, ofstream &outfile)
{
int requested_acct;
int result;
Account account;
cout << "\tType in the Account you wish to remove: ";
cin >> requested_acct;
result = bank.findAcct(requested_acct);
if(result == -1)
{
outfile << endl;
outfile << "\tTransaction: Deleting Account" << endl;
outfile << "\tError: Unable to find your account." << endl;
}
elseif(bank.closeAcct(result))
{
outfile << endl;
outfile << "\tTransaction: Deleting Account " << requested_acct;
outfile << endl;
bank.subtract_numaccts(account);
}
else
{
outfile << endl;
outfile << "\tTransaction: Deleting Account" << endl;
outfile << "\tError: There is still money within your account." << endl;
outfile << "\tPlease be sure to withdraw all funds before deleting" << endl;
}
return;
}
The boolean returns false if the Balance of the account does not equal 0.00, however even when the balance does equal zero, it still returns false. I'm still fairly new to C++ so please, any help will be wonderful.
If you are sure that if passes control flow to "return false" (you may check it adding "printf" for balance here, for example) then it is probable that you stumble against non-precise calculations and your Balance is not exact zero but some very close amount, like 1.5e-14
You should in such case compare not two values for equality, but abs of their difference to be smaller than for example 1e-7 etc.
However, I feel it is a sin to use floating point values for calculating money, except very rare cases.
Unless my results are hiding some extra numbers from me, this is what printed out:
Transaction: Withdrawal
Account number: 9478
Withdrawing: $25.00
New Balance: $0.00
Transaction: Deleting Account 9478
Error: There is still money within your account.
Please be sure to withdraw all funds before deleting
The current balance was $25.00 and using the transaction Withdrawal reduced it to $0.00. I am very befuddled..
There are several unknowns in your code - what is the return type of getBalance()? What does findAcct do?
One improvement in code - else clause on line 21 should have braces. Otherwise only line 22 is considered as part of else clause. Although I am not sure what would happen in this case since line 22 is another if clause. I would add braces to be more clear in this case.
Okay, I thought it was strange that this function worked fine in another program, but didn't for this. When I subtracted that withdrawal from the balance it worked, but didn't retain. I will have to play around with this some more. Thank you for answering rodiongork and funprogrammer :)