Bool within if statement not working

Hi! I'm having a bit of trouble trying to get this boolean expression to work. This is the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  bool Bank::closeAcct(int result)
{
    if(account[result].getBalance() != 0.00)
        return false;

    else
    {
        for(int n = result; n < numaccts;n++)
        {
            account[n].setDepositor(account[n+1].getDepositor().getName().getFirstname());
            account[n].setDepositor(account[n+1].getDepositor().getName().getLastname());
            account[n].setDepositor(account[n+1].getDepositor().getSSN());
            account[n].setAcctNum(account[n+1].getAcctNum());
            account[n].setAcctType(account[n+1].getAcctType());
            account[n].setBalance(account[n+1].getBalance());
        }
    }
    return true;
}


And this is the main from where I'm calling it:
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

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;
    }

        else
            if(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..
rodiongork is correct. It could be related to double comparison in your code. Here is a link to comparing doubles - http://stackoverflow.com/questions/12278523/comparing-double-values-in-c

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.
Last edited on
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 :)
Topic archived. No new replies allowed.