Fstream help

I made a log in sort of program with fstream. Basically you choose a username and a password and they get written into two separate txt files. I used the ios::app function to allow the program to edit the files with new usernames and passwords , but I don't know how to make the program cycle trough them inside the file , it only checks the last entries. Can anyone help me with this ? Thanks!
Hey Raul4pk,
http://www.cplusplus.com/forum/beginner/208583/ <- I took a peek back at your last post where you asked about similar code. Could you post your updated code? It sounds to me that your variables are getting clobbered by new input each time the loop repeats, so you are left with the last variables in the file.

You need to handle those inputs inside the loop or save them in separate variables each time for later use outside of the loop (which can be done with an array or with vectors).
Last edited on
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
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <fstream>
#include <cstring>
#include <ctime>
using namespace std;

int main ()
{
    cout<<"Welcome to Banca Muntenia";
    e3:
    cout<<"\n\n1.Create bank account\n2.Log into your bank account\n3.Exit"<<endl;
    int dec,e;
    string fname,sname,gen;
    char sex;
    e1:
    cin>>dec;
    if (dec==1)
    {
        cout<<"Enter your first name :";
        cin>>fname;
        cout<<"Enter your second name :";
        cin>>sname;
        e2:
        cout<<"Enter your gender (M/F) :";
        cin>>sex;
        if (sex=='M'|| sex=='m')
        {
            gen="Mr.";
        }
        else if (sex=='F'|| sex=='f')
        {
            gen="Mrs.";
        }
        else
        {
            cout<<"Please enter a valid gender option !"<<endl;
            goto e2;
        }
        int iban,iban1,iban2,iban3,iban4,iban5,iban6,iban7,iban8;
        srand(time(NULL));
        iban1=(rand()%10);
        iban2=(rand()%10);
        iban3=(rand()%10);
        iban4=(rand()%10);
        iban5=(rand()%10);
        iban6=(rand()%10);
        iban7=(rand()%10);
        iban8=((rand()%9)+1);
        iban=iban1*10000000+iban2*1000000+iban3*100000+iban4*10000+iban5*1000+iban6*100+iban7*10+iban8;
        int pin1,pin2,pin3,pin4,pin;
        pin1=(rand()%10);
        pin2=(rand()%10);
        pin3=(rand()%10);
        pin4=((rand()%9)+1);
        pin=pin1*1000+pin2*100+pin3*10+pin4;
        ofstream ibantxt;
        ofstream pintxt;
        ofstream fnametxt;
        ofstream snametxt;
        ofstream gentxt;
        ibantxt.open ("ibantxt.txt" , ios::app);
        ibantxt << "ROBM" << iban << std :: endl ;
        ibantxt.close();
        pintxt.open ("pintxt.txt" , ios::app);
        pintxt << pin << std :: endl ;
        pintxt.close();
        fnametxt.open ("fname.txt" , ios::app);
        fnametxt << fname << std :: endl ;
        fnametxt.close();
        snametxt.open ("sname.txt", ios::app);
        snametxt << sname << std :: endl ;
        snametxt.close();
        gentxt.open ("gender.txt", ios::app);
        gentxt << gen << std :: endl ;
        gentxt.close();
        cout<<gen<<fname<<" "<<sname<<",your account has been created succesfully!\nYour IBAN is : ROBM"<<iban<<" and your PIN code is :"<<pin<<endl;
        cout<<"You can use your IBAN and PIN to access your account at any time using an ATM"<<endl;
        cout<<"Have a nice day !"<<endl;
        goto e3;
    }
    else if (dec==2)
    {
        char readiban[40],readpin[40],readfname[40],readsname[40],readgender[40];
        ifstream readibantxt;
        ifstream readpintxt;
        ifstream readfnametxt;
        ifstream readsnametxt;
        ifstream readgendertxt;
        readibantxt.open ("ibantxt.txt");
        readibantxt >> readiban;
        readpintxt.open ("pintxt.txt");
        readpintxt >> readpin;
        readfnametxt.open ("fname.txt");
        readfnametxt >> readfname;
        readsnametxt.open ("sname.txt");
        readsnametxt >> readsname;
        readgendertxt.open ("gender.txt");
        readgendertxt >> readgender;
        char enteriban[30],enterpin[30];
        e5:
        cout<<"\nEnter your IBAN :";
        cin>>enteriban;
        if (strcmpi(enteriban,readiban)==0)
        {
            goto e4;
        }
        else
        {
            cout<<"This IBAN does not exist !"<<endl;
            goto e5;
        }
        e4:
            int j=0;
            while (strcmpi(enterpin,readpin)!=0)
            {
            cout<<"\nEnter your PIN code : ";
            cin>>enterpin;
            j++;
            if (strcmpi(enterpin,readpin)==0)
            {
                goto e6;
            }
            if (j==3)
            {
                cout<<"Too many failed attempts ! Account locked !"<<endl;
                cin>>e;
                return 0;
            }
            if (strcmpi(enterpin,readpin)!=0)
            {
                cout<<"Wrong PIN ! "<<3-j<<" attempts remaining !"<<endl;
            }
            }
            e6:
                cout<<"Welcome back , "<<readgender<<" "<<readfname<<" !"<<endl;
                e7: int sum;
                    ifstream readsumtxt;
                    readsumtxt.open ("sum.txt");
                    readsumtxt >> sum;
                cout<<"\nAccount ballance : $"<<sum<<endl;
                cout<<"1.Add funds\n2.Withdraw\n3.Exit"<<endl;
                int dec2;
                cin>>dec2;
                if (dec2==1)
                {
                    int add;
                    e9:
                    cout<<"Introduce the sum you want to add into the ATM :";
                    cin>>add;
                    if (add<0)
                    {
                        cout<<"You can't add that sum !"<<endl;
                        goto e9;
                    }
                    sum=sum+add;
                    ofstream sumtxt;
                    sumtxt.open ("sum.txt");
                    sumtxt << sum;
                    sumtxt.close ();
                    goto e7;
                }
                else if (dec2==2)
                {
                    int withdraw;
                    e8:
                    cout<<"Select the sum you would like to withdraw : ";
                    cin>>withdraw;
                    if (withdraw>sum)
                    {
                        cout<<"Insufficient funds !"<<endl;
                        goto e8;
                    }
                    if (withdraw<0)
                    {
                        cout<<"You can't withdraw that !"<<endl;
                        goto e8;
                    }
                    sum=sum-withdraw;
                    ofstream sumtxt;
                    sumtxt.open ("sum.txt");
                    sumtxt << sum;
                    sumtxt.close ();
                    goto e7;
                }
                else if (dec2==3)
                {
                    return 0;
                }
                else
                {
                    cout<<"Please choose a valid option !"<<endl;
                    goto e7;
                }
    }
    else if (dec==3)
    {
        cout<<"Have a nice day !"<<endl;
        cin>>e;
        return 0;
    }
    else
    {
        cout<<"Please enter a valid choice !"<<endl;
        goto e1;
    }
}


This is the code of the whole program. It basically generates a random IBAN and PIN code for your bank account and writes them to the files. The program allows for more than one IBAN , PIN, name etc to be stored in the files on separate lines but it only reads the most recent ones I think.
Hello Raul4pk,

There are several problems with your program.

First the use of "goto"s is bad form and poor programming. The "goto" is an unconditional jump to another part of the program. Which is not always a good idea. If you are going to use "goto"s your "goto" labels should be as far left as possible so they are easier to find. I would also consider using a better name than "d1" etc. as it is hard to figure out what is happening. "do/while" or a "while" loop can replace the "goto"s in most places.

You should initialize all your variables before you use them. I found when doing an add to the account that "sum" started with a large negative number an ended with a slightly smaller number. Not what you would want.

Using "int"s for your numbers will work, but somewhere in the future you may have to deal with a decimal number and "int"s will not work. Using a "double" would be a better choice.

The program allows for more than one IBAN , PIN, name etc to be stored in the files on separate lines but it only reads the most recent ones I think.

What is happening here is that each time you open a file to read the file pointer is at the beginning. It only reads the first line and then you close the file. If what you need is beyond the first line you never read it.

Your header file includes include "string.h" and "cstring". "cstring" includes "string.h" which makes the #include <string.h> unnecessary. What you really need is #include <string> and use std::string instead of character arrays. Then the lines like if (strcmpi(enterpin,readpin) == 0) can be more easily written as if (enterpin == readpin) and using "std::string"s have more versatility than character arrays.

Instead of trying to read files each time you need to I would consider reading the files into a vector (include header file vector) or a std::array (include header file array) my choice would be a vector. This way you could loop through the vector instead of opening a file and reading through it each time you need to.

My thoughts for now. Still figuring out how your program works.

Hope that helps,,

Andy
Topic archived. No new replies allowed.