transfer money

I'm a beginner on learning C++
have given a assignment for designing the transfer money to account
can anyone tell me how to use array and pointers in this sourcecode?
'coz i'm not doing well in 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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
#include <cstdlib>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main()
{
    ifstream acc;
    acc.open ("transfer.txt");
    int acc_no1,acc_no2,acc_no3,acc_no4,acc_no5,acc_no6,acc_no7,acc_no8;    
    acc>>acc_no1>>acc_no2>>acc_no3>>acc_no4>>acc_no5>>acc_no6>>acc_no7>>acc_no8;
    acc.close();    
    
    ifstream bal;
    bal.open("balance.txt");
    int bal1,bal2,bal3,bal4,bal5,bal6,bal7,bal8;
    bal>>bal1>>bal2>>bal3>>bal4>>bal5>>bal6>>bal7>>bal8;
    bal.close();  
    
    int enter_acc;
    cout<<"welcome"<<endl;
  
    
    do{
    cin>>enter_acc; 

    if (enter_acc==acc_no1)
    {
                           
        int money;
        char ans;
        do{
        cout<<"Transfer to account?"<<endl;
        cin>>enter_acc;
        cout<<"Please enter the amount you need to transfer: RM";
        cin>>money;
      
        if (enter_acc==acc_no2)
        {bal2=bal2+money;
        bal1=bal1-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no2<<endl;
        }        
        if (enter_acc==acc_no3)
        {bal3=bal3+money;
        bal1=bal1-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no3<<endl;
        }        
        if (enter_acc==acc_no4)
        {bal4=bal4+money;
        bal1=bal1-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no4<<endl;
        }        
        if (enter_acc==acc_no5)
        {bal5=bal5+money;
        bal1=bal1-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no5<<endl;
        }        
        if (enter_acc==acc_no6)
        {bal6=bal6+money;
        bal1=bal1-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no6<<endl;
        }        
        if (enter_acc==acc_no7)
        {bal7=bal7+money;
        bal1=bal1-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no7<<endl;
        }        
        if (enter_acc==acc_no8)
        {bal8=bal8+money;
        bal1=bal1-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no8<<endl;
        }        
        cout<<"Want to do the same transaction?(Y/N)"<<endl;
        cin>>ans;
        cout<<endl;
        }while(ans=='Y'||ans=='y');
        break;        
        }

    if (enter_acc==acc_no2)
    {
        int money;
        char ans;
        do{
        cout<<"Transfer to account?"<<endl;
        cin>>enter_acc;
        cout<<"Please enter the amount you need to transfer: RM";
        cin>>money;
      
        if (enter_acc==acc_no1)
        {bal1=bal1+money;
        bal2=bal2-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no1<<endl;
        }        
        if (enter_acc==acc_no3)
        {bal3=bal3+money;
        bal2=bal2-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no3<<endl;
        }        
        if (enter_acc==acc_no4)
        {bal4=bal4+money;
        bal2=bal2-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no4<<endl;
        }        
        if (enter_acc==acc_no5)
        {bal5=bal5+money;
        bal2=bal2-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no5<<endl;
        }        
        if (enter_acc==acc_no6)
        {bal6=bal6+money;
        bal2=bal2-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no6<<endl;
        }        
        if (enter_acc==acc_no7)
        {bal7=bal7+money;
        bal2=bal2-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no7<<endl;
        }        
        if (enter_acc==acc_no8)
        {bal8=bal8+money;
        bal2=bal2-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no8<<endl;
        }        
        cout<<"Want to do the same transaction?(Y/N)"<<endl;
        cin>>ans;
        cout<<endl;
        }while(ans=='Y'||ans=='y');
        break;        
        }

    if (enter_acc==acc_no3)
    {
        int money;
        char ans;
        do{
        cout<<"Transfer to account?"<<endl;
        cin>>enter_acc;
        cout<<"Please enter the amount you need to transfer: RM";
        cin>>money;
      
        if (enter_acc==acc_no2)
        {bal2=bal2+money;
        bal3=bal3-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no2<<endl;
        }        
        if (enter_acc==acc_no1)
        {bal1=bal1+money;
        bal3=bal3-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no1<<endl;
        }        
        if (enter_acc==acc_no4)
        {bal4=bal4+money;
        bal3=bal3-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no4<<endl;
        }        
        if (enter_acc==acc_no5)
        {bal5=bal5+money;
        bal3=bal3-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no5<<endl;
        }        
        if (enter_acc==acc_no6)
        {bal6=bal6+money;
        bal3=bal3-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no6<<endl;
        }        
        if (enter_acc==acc_no7)
        {bal7=bal7+money;
        bal3=bal3-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no7<<endl;
        }        
        if (enter_acc==acc_no8)
        {bal8=bal8+money;
        bal3=bal3-money;
        cout<<"Transaction complete!"<<endl;
        cout<<"Transfer amount :RM"<<money<<" to account "<<acc_no8<<endl;
        }        
                cout<<"Want to do the same transaction?(Y/N)"<<endl;
        cin>>ans;
        cout<<endl;
        }while(ans=='Y'||ans=='y');
        break;        
        }

    
    else
    {cout<<"Enter the account again."<<endl;} 
  
    }while (enter_acc!=acc_no1&&enter_acc!=acc_no2&&enter_acc!=acc_no3&&enter_acc!=acc_no4&&enter_acc!=acc_no5&&enter_acc!=acc_no6&&enter_acc!=acc_no7&&enter_acc!=acc_no8);  

    ofstream balance;
    balance.open ("balance.txt");
    balance << bal1 << endl;
    balance << bal2 << endl;
    balance << bal3 << endl;
    balance << bal4 << endl;
    balance << bal5 << endl;
    balance << bal6 << endl;
    balance << bal7 << endl;
    balance << bal8 << endl;
    balance.close();




        
    system ("PAUSE");
    

}

Instead of declaring int acc_no1, acc_no2, ... acc_no8; you could declare just int acc_[8]; (the name should be different from 'acc' declared as ifstream).
To get an 'acc_' value, remember that the first value will be acc_[0];,
not acc_[1].
Last edited on
Now i shorten the code
but i have to key in "i" to call the account number.
Question: How to let the "i" be 1 by entering the account number only?

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

using namespace std;
int main()
{
    ifstream acc;
    acc.open ("transfer.txt");
    int acc_no[8];    
    acc>>acc_no[0]
       >>acc_no[1]
       >>acc_no[2]
       >>acc_no[3]
       >>acc_no[4]
       >>acc_no[5]
       >>acc_no[6]
       >>acc_no[7];   
    acc.close();    
    
    ifstream bal;
    bal.open ("balance.txt");
    int bal_no[8];    
    bal>>bal_no[0]
       >>bal_no[1]
       >>bal_no[2]
       >>bal_no[3]
       >>bal_no[4]
       >>bal_no[5]
       >>bal_no[6]
       >>bal_no[7];   
    bal.close();    
    
    int i,j,money,account;
    i=2;
    cout<<"Your account number "<<i<<" is "<<acc_no[i]<<endl;
    cout<<"Enter the account number you wish to transfer"<<endl;
    cin>>j;
    cout<<"The account number "<<j<<" is "<<acc_no[j]<<endl;
    cout<<"Enter the amount you wish to transfer: RM";
    cin>>money;
    bal_no[i]=bal_no[i]-money;
    bal_no[j]=bal_no[j]+money;
    cout<<bal_no[i]<<endl;
    cout<<bal_no[j]<<endl;
    cout<<money<<endl;
    
  
    ofstream balance;
    balance.open ("balance.txt");
    balance << bal_no[0] << endl;
    balance << bal_no[1] << endl;
    balance << bal_no[2] << endl;
    balance << bal_no[3] << endl;
    balance << bal_no[4] << endl;
    balance << bal_no[5] << endl;
    balance << bal_no[6] << endl;
    balance << bal_no[7] << endl;
    balance.close();

    system ("PAUSE");
    
}
 
Last edited on
Just to help shorten the code even more, why don't you try some 'for' loops?

for (i = 0; i < 8 (in this case); i++)
your code here;
Topic archived. No new replies allowed.