Getting a value subtracted twice

What am I missing here? This is an assignment. I can't seem to find the error.
Please help

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
  case 'C':
case 'c':
	cout << fixed << showpoint << setprecision(2) << endl;
	cout << "\n\nCHECKING account " << CAnumber << " total is: " 
             << CAtotal << endl; 
	cout << "\n\tPlease enter the amount you"
	     << " would like to withdrawl: ";
	cin >> temp;
	
	cout << "\n\tYou have chosen to withdrawl " 
		<< temp << ".";

	check.transactionsC();
	recTran();
						
	if ((CAtotal -=temp) > 0)//<--when accessed, it subtracts twice
	{
		CAtotal -=temp;
		cout << "\n\tYour new total is: " << CAtotal<<endl;
	}
		
	else if ((CAtotal -=temp) <= 0)
	{
		insufficent();
		CAtotal += fee;			
	}
else if (temp >= 10000)
		{
			cout << "\nWOW!!!That's quite a bit of pocket cash!!\n"
				<< "do you really need that much!?!?(It's a simulator.)\n";
		}
		break;
	default:
		cout << "\n\n\tttINVALID SELECTION, PLEASE TRY AGAIN";
		break;
	}
}


other functions called
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void CheckingAccount::transactionsC()
{
	tran++;
	recTran();
	fee = 5.00;
	if (tran >= 4)
	{
	withdrawl();
	CAtotal -= fee;
	cout << "\tA transaction fee of $" << fee
		<< " has been withdrawl\n"
		<< "\tfor (3) or more transactions, you have\n"
		<< "\treached " << tran << " transactions"
		<< "\ton this CHECKING account.\n";
	}
}


1
2
3
4
void bankingAccount::recTran()
{
	tran++;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void bankingAccount::insufficent()
{
	do
		{
		cout << "\n**There are insufficent funds in this savings account**\n"
			<< "**Please deposit additions funds.**\n\n";
		}
	while (SAtotal <= 0);
	
	do 
		{
			cout << "\n**There are insufficent funds in this checking account**\n"
			<< "**Please deposit additions funds.**\n\n";
		}
	while (CAtotal <= 0);
}
Last edited on
You do remember that:
1
2
3
CAtotal -=temp
// is equivalent to
CAtotal = CAtotal - temp

You change the CAtotal within the condition (potentially in both of them). Is that really what you intend to do?

Otherwise, do you spot something possibly unnecessary in:
1
2
if ( a > 0 ) {...}
else if ( a <= 0 ) {...}
@ keskiverto - Oh... I see what I did. Thanks for that. I wasn't thinking. I deleted the subtraction in the condition, and it worked flawlessly.
1
2
3
4
5
6
7
8
9
10
11
if (CAtotal  > 0)
		{
			CAtotal -=temp;
			cout << "\n\tYour new total is: " << CAtotal<<endl;
		}
		
		else if (CAtotal <= 0)
		{
			insufficent();
			CAtotal += fee;			
		}
Last edited on
I have a feeling that you should recheck the logic of your program carefully. There might be inputs that result in anything but flawless behaviour.
Topic archived. No new replies allowed.