Why isn't it reading and writing to the file?

Why isn't it writing to the file? is there something I've missed?
Please point me out the problem and how could I fix 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//Bank Managment
#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <dos.h>
#include <stdlib.h>
#include <conio.h>

using namespace std;

    void addrecord();
    void disprecord();
    void delrecord();
    void modrecord();
    void withdraw();
    void deposit();
    float w,z;

class account
{
    int accountno,cls;
    char name[30],type;
    float deposit,withdraw;

    public:
    account()
    {
        deposit=withdraw=0;
    }
    int giveaccountno()
    {
        return accountno;
    }
    void getdata(int mrno)
    {
        accountno=mrno+1;
        cout<<"ACCOUNT NUMBER:: ";
        cin>>accountno; //<<" ";
        cout<<"ENTER YOUR NAME:: ";
        cin>> name;
        cout<<" ";
        cout<<"ENTER TYPE OF ACCOUNT SAVING(s)/CURRENT(c):: ";
        cin>>type;
        cout<<" ";
        cout<<"ENTER INITIAL AMOUNT::Bs.F  ";
        cin>>deposit;
        cout<<" ";
    }

    void withdrawal(int m)
    {
        cout<<"AMOUNT BEFORE WITHDRAWING::Bs.F  "<<deposit<<" ";
        deposit=deposit-m;
        cout<<"AMOUNT AFTER WITHDRAWING::Bs.F  "<<deposit;
    }

    void deposital(int m)
    {
        cout<<"AMOUNT BEFORE DEPOSIT::Bs.F  "<<deposit<<" ";
        deposit=deposit+m;
        cout<<"AMOUNT AFTER DEPOSIT ::Bs.F  "<<deposit;
    }

    void dispdata()
    {
        int scrnt=0;
        if(scrnt==1)
        {
            system("CLS");
            cout<<"DISPLAY ALL MENU";
            scrnt=0;
        }
        cout<<"ACCOUNT NUMBER:: ";
        cout<<accountno;
        cout<<"NAME OF DEPOSITER:: ";
        cout<<name;
        cout<<"TYPE OF ACCOUNT SAVING(s)/CURRENT(c):: ";
        cout<<type;
        cout<<"BALANCE::Bs.F ";
        cout<<deposit;
        scrnt++;
    }
};

void addrecord()
{
    account obj_1,obj_2;
    fstream fout;
    fout.open("banking.txt",ios::in|ios::binary);
    if(!fout)
    {
        cout<<"FILE OPEN ERROR  ";
        getch();
        return;
    }

    int recsize=sizeof(account);
    fout.seekg(0,ios::end);
    fout.seekg(-1*recsize,ios::cur);
    fout.read((char*)&obj_1,recsize);
    int mrno=obj_1.giveaccountno();
    fout.close();
    system("CLS");
    cout<<"ADD MENU\n";
    obj_2.getdata(mrno);
    fout.open("banking.txt",ios::app|ios::binary);

    if(!fout)
    {
        cout << "FILE OPEN ERROR  ";
        getch();
        return;
    }
    fout.write((char*)&obj_2,recsize);
    cout << "RECORD ADDED TO DATABASE\n" << "Press any key to continue... ";
    getch();
    fout.close();
}

void disprecord()
{
    account obj_4;
    fstream fout;
    int mrno,flag=0;
    int recsize=sizeof(account);
    system("CLS");
    cout<<"DISPLAY A RECORD MENU";
    fout.open("banking.txt",ios::in);

    if(!fout)
    {
        cout<<"FILE OPEN ERROR  ";
        getch();
        return;
    }

    cout<<"ENTER  THE ACCOUNT NUMBER  ";cin>>mrno;

    while(fout.read((char*)&obj_4,recsize))
    {
        if (obj_4.giveaccountno()==mrno)
        {
            obj_4.dispdata();
            cout<<"Press any key.....";
            flag=1;break;
        }
    }

    if(flag==0)
    {
        cout<<"NO SUCH ACCOUNT EXIST  ";
        cout<<"Press any key......";
    }

    getch();
    fout.close();
}

void delrecord()
{
    account obj_5;
    fstream fout,temp;
    int mrno,flag;
    int recsize=sizeof(account);
    system("CLS");
    cout<<"CLOSE ACCOUNT MENU";
    fout.open("banking.txt",ios::in);

    if(!fout)
    {
        cout<<"FILE OPEN ERROR  ";
        getch();
        return;
    }

    temp.open("temp.txt",ios::app|ios::binary);
    if(!temp)
    {
        cout<<"FILE OPEN ERROR  ";
        getch();
        return;
    }

    cout<<"ENTER THE ACCOUNT NUMBER ";
    cin>>mrno;
    while(fout.read((char*)&obj_5,recsize))
    {
        if(obj_5.giveaccountno()==mrno)
        {
            obj_5.dispdata();
            char confirm;
            cout<<"ARE YOU SURE TO DELETE IT(Y/N)..";cin>>confirm;
            if(confirm=='Y'||confirm=='y')
            {
                fout.read((char*)&obj_5,recsize);
                cout<<"RECORD DELETED FORM DATABASE";
                cout<<"press any key....";
                flag=1;
                if(!fout)
                break;
            }
            flag=1;
        }
    temp.write((char*)&obj_5,recsize);}
    fout.close();
    temp.close();
    remove("banking.txt");
    rename("temp.txt","banking.txt");
    if(flag==0)
    {
        cout<<"NO SUCH ACCOUNT EXIST";
        cout<<"Press any key.....";
    }
    getch();
}

void modrecord()
{
    account obj_6;
    fstream fout;
    int mrno,flag=0;
    int recsize=sizeof(account);
    system("CLS");
    cout<<"MODIFY RECORD MENU";
    fout.open("banking.txt",ios::in|ios::out|ios::binary);
    if(!fout)
    {
        cout<<"FILE OPEN ERROR  ";
        getch();
        return;
    }

    fout.seekg(ios::beg);
    cout<<"ENTER RECORD NUMBER  ";
    cin>>mrno;
    while(fout.read((char*)&obj_6,recsize))
    {
        if(obj_6.giveaccountno()==mrno)
        {
            system("CLS");
            cout<<"MODIFY MENU";
            obj_6.dispdata();
            int tmprno=obj_6.giveaccountno()-1;
            account obj_7;
            cout<<"ENTER NEW DATA";
            obj_7.getdata(tmprno);
            char confirm;
            cout<<"ARE YOU SURE(Y/N)";
            cin>>confirm;
            if(confirm=='Y'||confirm=='y')
            {
                fout.seekg(-1*recsize,ios::cur);
                fout.write((char*)&obj_7,recsize);
                cout<<"RECORD MODIFIED";
                cout<<"Press any key.....";
                flag=1;
            }
        }
        if(flag==0)
        {
            cout<<"NO SUCH RECORD EXIST";
            cout<<"Press any key.....";
        }
    }
    fout.close();
    getch();
}

void withdraw()
{
    account obj_9;
    fstream fout;
    int mrno=0;
    int recsize=sizeof(account);
    system("CLS");
    cout<<"WITHDRAWAL MENU";
    fout.open("banking.txt",ios::in|ios::out|ios::binary);
    if(!fout)
    {
        cout<<"FILE OPEN ERROR  ";
        getch();
        return;
    }

    fout.seekg(ios::beg);
    cout<<"ENTER ACCOUNT NUMBER:: ";
    cin>>mrno;
    while(fout.read((char*)&obj_9,recsize))
    {
        if(obj_9.giveaccountno()==mrno)
        {
            system("CLS");
            cout<<"ENTER THE AMOUNT TO BE WITHDRAWED::Bs.F ";
            cin>>w;
            obj_9.withdrawal(w);
            fout.seekg(-1*recsize,ios::cur);
            fout.write((char*)&obj_9,recsize);
        }
    }
    fout.close();
    getch();
}

void deposit(void)
{
    account obj_10;
    fstream fout;
    int mrno=0;
    int recsize=sizeof(account);
    system("CLS");
    cout<<"DEPOSITAL MENU";
    fout.open("banking.txt",ios::in|ios::out|ios::binary);
    if(!fout)
    {
        cout<<"FILE OPEN ERROR  ";
        getch();
        return;
    }

    fout.seekg(ios::beg);
    cout<<"ENTER ACCOUNT NUMBER:: ";
    cin>>mrno;
    while(fout.read((char*)&obj_10,recsize))
    {
        if(obj_10.giveaccountno()==mrno)
        {
            system("CLS");
            cout<<"ENTER THE AMOUNT TO BE DEPOSITED::Bs.F ";
            cin>>w;
            obj_10.deposital(w);
            fout.seekg(-1*recsize,ios::cur);
            fout.write((char*)&obj_10,recsize);
        }
    }
    fout.close();
    getch();
}

int main()
{
    int menu;

    do
    {
        cout<<"MAIN MENU." << endl;
        cout<<"1.NEW ACCOUNT." << endl;
        cout<<"2.CLOSE AN ACCOUNT." << endl;
        cout<<"3.MODIFY AN ACCOUNT." << endl;
        cout<<"4.DISPLAY AN ACCOUNT." << endl;
        cout<<"5.WITHDRAW AMOUNT." << endl;
        cout<<"6.DEPOSIT AMOUNT."<< endl;
        cout<<"7.EXIT." << endl;
        cout<<"ENTER YOUR CHOICE." << endl;
        cin >> menu;

        switch (menu)
        {
            case 1:
            addrecord();
            break;

            case 2:
            delrecord();
            break;

            case 3:
            modrecord();
            break;

            case 4:
            disprecord();
            break;

            case 5:
            withdraw();
            break;

            case 6:
            deposit();
            break;
        }
    }
    while (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5 && menu != 6 && menu != 7);

    return 0;
}
reevaluate your void addrecord() this is where your error is being thrown.
Topic archived. No new replies allowed.