Help with if/else

I am trying to write a program to do the following and I cannot seem to get it to work correctly. Could anybody help me write this code to work correctly or at least spot the errors? I have gone over it multiple times and I cannot figure out why it isn't working. Any help is greatly appreciated. Thanks

This is a problem with an input file (bank.dat) that looks like this:
10245 2 50.00 867.23
10672 1 100.00 125.76
30403 1 1023.54 3456.98
20034 3 345.65 650.89
10334 2 567.87 436.56
29776 2 400.00 450.00
31123 2 5678.00 9304.56
11267 2 500.00 2.00
32001 2 498.00 556.89
10998 2 340.00 350.00
10003 6 112.00 998.56
29984 2 23.40 78.43
25650 2 456.78 456.78
32761 2 234.56 236.78
10099 1 65789.34 36.98
22222 2 4.56 8.23

The first number is the account number, second number is the code: 1= deposit 2=withdrawal 3=invalid code, 6=invalid code, third number is the amount of the withdrawal/deposit, and last number is the balance before the transaction. I am trying to loop through the data file and perform the deposits, withdrawal if they have enough funds, charge a fee of 10.00 if their ending balance after the transaction is under 100.00. Then output the total withdrawals and total amount withdrawn, and do the same with the deposits.

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

using namespace std;
void main ()

{ int acctno, code, dep=0, wit=0;
  float amount, balance, endbalance=0,deptot=0, wtot=0;
  
ifstream fin;
ofstream fout;
fin.open("Bank.dat");
fout.open("totals.txt");
fout.setf (ios::fixed);
fout.setf (ios::showpoint);
fout.precision (2);
  
fin>>acctno>>code>>amount>>balance;
 while (!fin.eof()) 
 {
if(code=1)
endbalance=balance+amount;
fout<<acctno<<" Your updated balance is "<<endbalance<<". Have a nice day."<<endl;
deptot=amount+deptot;
dep=dep+1;
else	
if(code=2)
{if (amount>balance)
{fout<<acctno<<" insufficient funds"<<endl;
fin>>acctno>>code>>amount>>balance;}
		
else
(amount<=balance)
{endbalance=balance-amount;
		}	
		
else
if(endbalance>=100.00)
{fout<<acctno<<endbalance<<" Have a nice day"<<endl;
wtot=amount+wtot;
wit++;
fin>>acctno>>code>>amount>>balance;}
		
else(endbalance<100.00);
{endbalance=endbalance-10.00;
fout<<acctno<<" balance below $100.00 minimum -- $10.00 fee accessed"<<endbalance<<endl;
wtot=amount+wtot;
wit++;
fin>>acctno>>code>>amount>>balance;
}
}
}
if(code!=1 || 2);
{fout<<acctno<<" bad transaction code"<<endl;}
fin>>acctno>>code>>amount>>balance;

fout<<"There were "<<dep<<" deposits today."<<endl;
fout<<"There was "<<deptot<<" total deposited today."<<endl;
fout<<"There were "<<wit<<" withdrawals today.""<<endl;
fout<<"There was "<<wittot<<" total withdrawn today."<<endl;
}


 
Without actually trying to run this, I can see a few errors.

void main() should be int main()

Several if statements like this: if(code=1) change the assignment operator = to the equality operator ==

At line 53:
if(code!=1 || 2);
remove the semicolon, complete the test for code!=2 and change the || to &&
It should be
if (code !=1 && code != 2)

Also, controlling a loop with a test of eof() is generally a bad idea. If the input operation is unsuccessful for some reason, the eof flag may not be set. It is safer to test the status of fin after each input, either like this:
while (fin)
or perhaps more conveniently, do the input and test in one statement, like this:
1
2
3
4
while (fin>>acctno>>code>>amount>>balance)
{

}



One other thing. An else cannot have a condition attached to it like this:
else (amount<=balance)
there should be an if there,
else if (amount<=balance)
Last edited on
Topic archived. No new replies allowed.