ATM PIN problem

Hello! I have a function that lets the user enter the correct pin to enter ATM transactions.

My problem is that when I go to the second and third loop, it never reads a correct pin even if it is actually the correct pin input.

On the first input though, it is read perfectly well and can read a correct pin.

The correct pin comes from a separate lst file.

Am I missing some things? Please help me. Thanks so much in advance!

#include "atm.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

void ATM::verifyPIN()
{
    string pass="";
    int i;
    char pinNumber;
    string accountList = "E:/CpE 31 Projects/Toledo/acct.lst";
    ifstream accountFileIn;
    accountFileIn.open(accountList.data(), ios::in);
    if (accountFileIn.is_open())
    {
        accountFileIn >> ID >> PIN >> balance;
    }
    cout << "Enter PIN number: ";
    pinNumber = getch();
    while(pinNumber != 13)
    {
        pass.push_back(pinNumber);
        cout << '*';
        pinNumber = getch();
    }
    system("CLS");
    for (i = 2; i > 0; i--)
    {
        if(pass != PIN)
        {
            cout << "Incorrect PIN. Try again: ";
            pinNumber = getch();
            while(pinNumber != 13)
            {
                pass.push_back(pinNumber);
                cout << '*';
                pinNumber = getch();
            }
            system("CLS");
            if (i == 1 && pass != PIN)
            {
                    cout << "Number of attempts out. ATM will now close." << endl;
                    exit(1);
            }
            else if (i == 1 && pass == PIN)
            {
            }
        }
        else
        {
        }
    }
    accountFileIn.close();
}
Last edited on
Use code tags and it will show line numbers.

I can't run it to test, but there are a couple things I don't get.


char pinNumber;

&

while(pinNumber != 13)

& not sure about logic below.

1
2
3
    for (i = 2; i > 0; i--)
    {
        if(pass != PIN)


Try this, change the code to show the pin entered, on the tries that are not working and see what you get.

Edit, I think I see the problem you might change
int i;
to
int i=0;


Try this and you'll see why

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

void ATM()
{
	int i;
	i=i+1;
	cout << i << endl;
}

int main ()
{
ATM();
ATM();

return 0;
}
Last edited on
I believe I need to show the entire program.
 
char pinNumber;
is what I use because it happens to be the one that is read and not int or anything else.

 
while (pinNumber != 13)
is to keep the program from proceeding to the next command until Enter is pressed.

Header:

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
#ifndef ATM_H
#define ATM_H

#include <string>
using namespace std;

class ATM
{
private:
    string PIN;
    int ID;
    float balance;
public:
    void verifyPIN();
    void transaction();
    void balanceInquiry();
    void withdrawal();
    void changePIN();
    void fastCash();
    void deposit();
    void billsPayment();
    void overwrite();
    ATM();
};

#endif // ATM_H 
Source:
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
#include "atm.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

void ATM::verifyPIN()
{
    string pass="";
    int i;
    char pinNumber;
    string accountList = "C:/Users/SAMSUNG/Downloads/acct.lst";
    ifstream accountFileIn;
    accountFileIn.open(accountList.data(), ios::in);
    if (accountFileIn.is_open())
    {
        accountFileIn >> ID >> PIN >> balance;
    }
    cout << "Enter PIN number: ";
    while (pinNumber != 13)
    {
        //pass.push_back(pinNumber);

        pinNumber = getch();
        if(pinNumber!=13)
        {
            pass+=pinNumber;
            cout << "*";
        }
    }
    system("CLS");
    for (i = 2; i > 0; i--)
    {

        if(pass != PIN)
        {
            cout << "Incorrect PIN. Try again: ";
            cin >> pinNumber;
            system("CLS");
            if (i == 1 && pass != PIN)
            {
                    cout << "Number of attempts out. ATM will now close." << endl;
                    exit(1);
            }
            else if (i == 1 && pass==PIN)
            {
                transaction();
            }
        }
        else
        {
        }
    }
    accountFileIn.close();
}

void ATM::balanceInquiry()
{
    char choice1, u;
    string accountList = "C:/Users/jakem/Documents/Toledo/acct.lst";
    ifstream accountFileIn;
    accountFileIn.open(accountList.data(), ios::in);
    if (accountFileIn.is_open())
    {
        accountFileIn >> ID >> PIN >> balance;
    }
    cout << "Balance: " << balance << endl;
    cout << "Would you like another transaction? [y/n]" << endl;
    cin >> choice1;
    system("CLS");
    for (u = 2; u > 0; u--)
    if (choice1 == 'y')
    {
        transaction();
    }
    else if (choice1 == 'n')
    {
        exit(1);
    }
    else
    {
        cout << "Invalid choice. Try again: " << endl;
        cin >> choice1;
        system("CLS");
        if (u == 1 && choice1 != 'y' && choice1 != 'n')
        {
            cout << "Number of attempts out. ATM will now close." << endl;
            exit(1);
        }
        else if (u == 1 && choice1 == 'y')
        {
            transaction();
        }
        else if (u == 1 && choice1 == 'n')
        {
            exit(1);
        }
    }
    accountFileIn.close();
}

void ATM::withdrawal()
{
    float withdraw;
    float newBalance;
    cout << "Please enter the amount of money you would like to withdraw: ";
    cin >> withdraw;
    system("CLS");
    if (withdraw <= balance)
    {
        newBalance = balance - withdraw;
        cout << "Your new balance is now " << newBalance << endl;
        balance = newBalance;
        overwrite();
        exit(1);
    }
    else if (withdraw >= balance)
    {
        cout << "Insufficient funds! Returning to main menu." << endl;
        transaction();
    }
}

void ATM::fastCash()
{
    int exactWithdraw;
    float newBalance2;
    cout << "Select exact amount of money to be withdrawn." << endl;
    cout << "1. 1000" << endl;
    cout << "2. 3000" << endl;
    cout << "3. 5000" << endl;
    cin >> exactWithdraw;
    system("CLS");
    if (exactWithdraw == 1)
    {
        if (balance < 1000)
        {
            cout << "Insufficient funds! Returning to main menu." << endl;
            transaction();
        }
        else
        {
        newBalance2 = balance - 1000;
        cout << "Your new balance is now " << newBalance2 << endl;
        balance = newBalance2;
        overwrite();
        exit(1);
        }
    }
    else if (exactWithdraw == 2)
    {
        if (balance < 3000)
        {
            cout << "Insufficient funds! Returning to main menu." << endl;
            transaction();
        }
        else
        {
        newBalance2 = balance - 3000;
        cout << "Your new balance is now " << newBalance2 << endl;
        balance = newBalance2;
        overwrite();
        exit(1);
        }
    }
    else if (exactWithdraw == 3)
    {
        if (balance < 1000)
        {
            cout << "Insufficient funds! Returning to main menu." << endl;
            transaction();
        }
        else
        {
        newBalance2 = balance - 5000;
        cout << "Your new balance is now " << newBalance2 << endl;
        balance = newBalance2;
        overwrite();
        exit(1);
        }
    }
    else
    {
        cout << "Invalid choice! Returning to main menu." << endl;
        transaction();
    }
}

void ATM::deposit()
{
    float newBalance3;
    float depositMoney;
    cout << "Enter amount to money to be deposited: ";
    cin >> depositMoney;
    system("CLS");
    newBalance3 = balance + depositMoney;
    cout << "Your new balance is now " << newBalance3 << endl;
    balance = newBalance3;
    overwrite();
    exit(1);
}

void ATM::billsPayment()
{
    int select2;
    float payment;
    float newBalance4;
    cout << "Select section to pay" << endl;
    cout << "1. School tuition" << endl;
    cout << "2. Electric bill" << endl;
    cout << "3. Water bill" << endl;
    cin >> select2;
    system("CLS");
    if (select2 == 1)
    {
        cout << "Enter amount to be paid to school tuition: ";
        cin >> payment;
        system("CLS");
        if (payment >= balance)
        {
            cout << "Insufficient funds! Returning to main menu." << endl;
            transaction();
        }
        else
        {
            newBalance4 = balance - payment;
            cout << "Your new balance is now " << newBalance4 << endl;
            balance = newBalance4;
            overwrite();
            exit(1);
        }
    }
    else if (select2 == 2)
    {
        cout << "Enter amount to be paid to electric bill: ";
        cin >> payment;
        system("CLS");
        if (payment >= balance)
        {
            cout << "Insufficient funds! Returning to main menu." << endl;
            transaction();
        }
        else
        {
            newBalance4 = balance - payment;
            cout << "Your new balance is now " << newBalance4 << endl;
            balance = newBalance4;
            overwrite();
            exit(1);
        }
    }
    else if (select2 == 3)
    {
        cout << "Enter amount to be paid to school tuition: ";
        cin >> payment;
        system("CLS");
        if (payment >= balance)
        {
            cout << "Insufficient funds! Returning to main menu." << endl;
            transaction();
        }
        else
        {
            newBalance4 = balance - payment;
            cout << "Your new balance is now " << newBalance4 << endl;
            balance = newBalance4;
            overwrite();
            exit(1);
        }
    }
}

void ATM::transaction()
{
    int select;
    cout << "Select the service needed: " << endl;
    cout << "1:Balance Inquiry" << endl;
    cout << "2:Withdrawal" << endl;
    cout << "3:Change Pin" << endl;
    cout << "4:Fast Cash" << endl;
    cout << "5:Deposit" << endl;
    cout << "6:Bills Payment" << endl;
    cout << "7:Exit" << endl;
    cin >> select;
    system("CLS");
    switch (select)
    {
    case 1:
        balanceInquiry();
        break;
    case 2:
        withdrawal();
        break;
    case 3:
        changePIN();
        break;
    case 4:
        fastCash();
        break;
    case 5:
        deposit();
        break;
    case 6:
        billsPayment();
        break;
    case 7:
        exit(1);
        break;
    }
}

void ATM::overwrite()
{
    string accountList = "C:/Users/jakem/Documents/Toledo/acct.lst";
    ofstream accountFileOut;
    accountFileOut.open(accountList.data(), ios::trunc);
    if (accountFileOut.is_open())
    {
        accountFileOut << left
                       << setw(75) << ID
                       << setw(75) << PIN
                       << setw(75) << balance
                       << endl;
    }
    accountFileOut.close();
}



ATM::ATM()
{

}
Main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "atm.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ATM myATM;
    cout << "Welcome to Bank!" << endl;
    myATM.verifyPIN();
    myATM.transaction();
}
Topic archived. No new replies allowed.