Help with streams

I'm trying to write a bank program, and I would like to save the accounts that are created and make them accessible by the pin number they chose. The program asks for a first and last name, a 4 digit pin number, and a starting balance. The program then is supposed to create a text file named after the pin they chose, and print their full name and their balance. Then, when they enter their pin again, the program is supposed to grab the full name and balance and store them in the variables. But the program pauses after you enter a pin. Can anyone help me with this?

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

using namespace std;

int main()
{
    while (1 == 1)
    {
        cout << "Welcome to the C++ bank." << endl;
        cout << endl;
        
        cout << "Do you have an account with us? \"Y\" or \"N\": ";
        string choice;
        cin >> choice;
        cout << endl;
        
        string fname;
        string lname;
        string fullname = fname + " " + lname;
        int pin;
        int balance = 0;
        
        if (choice == "N" || choice == "n")
        {
            cout << "Enter your first name: ";
            string fname;
            cin >> fname;
            cout << endl;
            
            cout << "Enter your last name: ";
            string lname;
            cin >> lname;
            cout << endl;
            
            string fullname = fname + " " + lname;
            
            cout << "Enter a 4 digit pin: ";
            int pin;
            cin >> pin;
            cout << endl;
            
            cout << "Enter your pin again: ";
            int pin2;
            cin >> pin2;
            cout << endl;
            
            while (pin < 1000 || pin2 < 1000)
            {
                cout << "Your pin was not 4 digits long. Please try again." << endl;
                cout << endl;
                
                cout << "Enter a 4 digit pin: ";
                cin >> pin;
                cout << endl;
                
                cout << "Enter your pin again: ";
                cin >> pin2;
                cout << endl;
            }
            
            while (pin > 9999 || pin2 > 9999)
            {
                cout << "Your pin was not 4 digits long. PLease try again." << endl;
                cout << endl;
                
                cout << "Enter a 4 digit pin: ";
                cin >> pin;
                cout << endl;
                
                cout << "Enter your pin again: ";
                cin >> pin2;
                cout << endl;
            }
            
            while (pin != pin2)
            {
                cout << "Your pins did not match. Please try again." << endl;
                cout << endl;
                
                cout << "Enter a 4 digit pin: ";
                cin >> pin;
                cout << endl;
                
                cout << "Enter your pin again: ";
                cin >> pin2;
                cout << endl;
            }
            
            cout << "Please deposit your starting balance: ";
            int balance;
            cin >> balance;
            cout << endl;
            
            cout << "If you would like to withdraw, type \"W\". If you would like to deposit, type \"D\". If you would like to log out press any key: ";
            cin >> choice;
            cout << endl;
            
            if (choice == "W" || choice == "w")
            {
                cout << "Enter the amount you want to withdraw: ";
                int wamount;
                cin >> wamount;
                cout << endl;
                
                while (balance < wamount)
                {
                    cout << "You have insufficient funds. Enter a different amount: ";
                    cin >> wamount;
                    cout << endl;
                }
                
                balance = balance - wamount;
                
                cout << "Your new balance is: " << balance << endl;
                cout << endl;
            }
            
            else if (choice == "D" || choice == "d")
            {
                cout << "Enter the amount you would like to deposit: ";
                int damount;
                cin >> damount;
                cout << endl;
                
                balance = balance + damount;
                
                cout << "Your new balance is: " << balance << endl;
                cout << endl;
                cout << endl;
            }
            
            ofstream out_file (to_string(pin) + ".txt");
            out_file << fullname << '\n';
            out_file << balance << '\n';
            out_file.close();
            
            
        }
        
        else if (choice == "Y" || choice == "y")
        {
            cout << "Enter your 4 digit pin: ";
            cin >> pin;
            cout << endl;
            
            string line;
            ifstream in_file (to_string(pin) + ".txt");
            if (in_file.is_open())
            {
                while (getline (in_file, line))
                {
                    cin >> line;
                    fullname = line;
                    cin >> line;
                    int result;
                    istringstream convert(line);
                    if ((!convert >> result))
                    {
                        result = 0;
                    }
                    balance = result;
                    
                    in_file.close();
                }
            }
            
            else
            {
                cout << "Sorry, you do not have an account." << endl;
                continue;
            }
            
            cout << "Welcome " << fullname << "." << endl;
            cout << endl;
            
            cout << "You have " << balance << " in your account" << endl;
            cout << endl;
            
            cout << "If you would like to withdraw, type \"W\". If you would like to deposit, type \"D\". If you would like to log out press any key: ";
            cin >> choice;
            cout << endl;
            
            if (choice == "W" || choice == "w")
            {
                cout << "Enter the amount you want to withdraw: ";
                int wamount;
                cin >> wamount;
                cout << endl;
                
                while (balance < wamount)
                {
                    cout << "You have insufficient funds. Enter a different amount: ";
                    cin >> wamount;
                    cout << endl;
                }
                
                balance = balance - wamount;
                
                cout << "Your new balance is: " << balance << endl;
                cout << endl;
            }
            
            else if (choice == "D" || choice == "d")
            {
                cout << "Enter the amount you would like to deposit: ";
                int damount;
                cin >> damount;
                cout << endl;
                
                balance = balance + damount;
                
                cout << "Your new balance is: " << balance << endl;
                cout << endl;
                
            }
            
            ofstream out_file (to_string(pin) + ".txt");
            out_file << fullname << '\n';
            out_file << balance << '\n';
            out_file.close();
            
        }
    }
}
Topic archived. No new replies allowed.