Console Won't Close After Counter Reaches Certain #

Oct 17, 2013 at 11:12pm
I'm writing a program for a simple ATM machine. The only valid pin is 123. If the user enters an invalid pin three times, the program should display and message and then close. But it won't even get to the part where it displays a message. If the correct pin is entered everything is fine, but if the wrong pin is entered, it won't close, even after many attempts.
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
int main()
{   
    int count = 0;
    while (count <= 3)
    {
        int pin;
    
        cout << "*** Welcome to My ATM****\n";
        cout << "Please enter your PIN:";
        cin >> pin;
    
        if (pin == 123)
        {
            menu();
        }    
        if (pin != 123)
        {
            count ++;
            system("cls");
            main();
        }
        if (pin == 3)
        {
            cout << "Too many wrong PINs.";
            return 0;
        }
    }          
}
Oct 17, 2013 at 11:16pm
never call main()
Oct 17, 2013 at 11:21pm
you weren't using count. you were incrementing it but you used if (pin == 3) not if (count == 3)


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
int main()
{   
    int count = 0;
    while (count <= 3)
    {
        int pin;
    
        cout << "*** Welcome to My ATM****\n";
        cout << "Please enter your PIN:";
        cin >> pin;
    
        if (pin == 123)
        {
            menu();
        }    
        if (pin != 123)
        {
            count ++;
            system("cls");
            main();
        }
        if (count == 3)
        {
            cout << "Too many wrong PINs.";
            return 0;
        }
    }          
}
Oct 17, 2013 at 11:29pm
Thank you! I works perfectly now :).
Topic archived. No new replies allowed.