i'm trying to create a simple ATM program...it's a bit of headache

I'm trying to create a simple ATM program, to ask for pin input, to verify the pin against a set pin input limit of 3 times, doing some balance for the money remaining and displaying it. I'm having a problem creating a loop, or a do while loop, how to set and check the number of trials. it should be simple, no database included, just simple...I'm a beginner and i tried this code down here, it works but i wanted to know how i can make it better, more accurate and beautiful.

this is the code:

#include <iostream>
//#include <cstdlib>


using namespace std;

int main()
{
int PIN;
int Amount;
int Balance;
int PINAttempts;
int count;
cout << endl;
cout << "Welcome to our ATM services." << endl << endl;;

PINAttempts=3;

while (PINAttempts>0)
{
cout << "Please enter your PIN." << endl;
cin >> PIN;

if (PIN == 1234)
{
cout << "PIN entry suceessful." << endl << endl;
PINAttempts= -1; //just a trick
}
else
{
cout << "Wrong PIN.Please try again." << endl << endl;
PINAttempts--;
cout << "\nRemaining PIN Attempt chances: " << PINAttempts << endl <<endl;
}
if (PINAttempts == 0)
{
cout <<"You have exceeded the PIN attempt.Goodbye"<< endl <<endl;
}
if (PINAttempts == -1)
{
cout << "Please enter the amount to withdraw." << endl;
cin >> Amount;
cout << endl;
Balance=100000;
Balance=Balance-Amount;
cout << "You have withdrawn Ksh. " << Amount << "." << endl;
cout << "Your new Balance is Kshs. " << Balance << "." << endl << endl;
cout << "Thank you for using this ATM.Welcome back";
cout << endl;
}
}
//system("PAUSE");
main();
return 0;
}



all the help will be gladly appreciated
Please wrap your code next time with

that looks better. =)

But anyways. first of all on line 52 uncomment:

system("pause"); <-- pause should be lower cased.

on line 53 get rid of:

main();

Here is also wrong.

1
2
3
4
if (PINAttempts == 0)
{
cout <<"You have exceeded the PIN attempt.Goodbye"<< endl <<endl;
}


since you want this to stop the program right here. have it return a number.

Also, your while loop will do all of this no matter what. Your program will not have the person reenter the PIN if invalid. You must add another loop in there to test the PIN repeatedly.


It'll look like 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
#include <iostream>
//#include <cstdlib>


using namespace std;

int main()
{
int PIN;
int Amount;
int Balance;
int PINAttempts;
int count;
cout << endl;
cout << "Welcome to our ATM services." << endl << endl;;

PINAttempts=3;

while (PINAttempts>0)
{

bool x;


do{
cout << "Please enter your PIN." << endl;
cin >> PIN;     
     
if (PIN == 1234)
{
cout << "PIN entry suceessful." << endl << endl;
PINAttempts= -1; //just a trick
x = true;
}
else
{
cout << "Wrong PIN.Please try again." << endl << endl;
PINAttempts--;
cout << "\nRemaining PIN Attempt chances: " << PINAttempts << endl <<endl;
x = false;
}
if (PINAttempts == 0)
{
cout <<"You have exceeded the PIN attempt.Goodbye"<< endl <<endl;
system("pause");
return -1;
}
}while(x != true);

if (PINAttempts == -1)
{
cout << "Please enter the amount to withdraw." << endl;
cin >> Amount;
cout << endl;
Balance=100000;
Balance=Balance-Amount;
cout << "You have withdrawn Ksh. " << Amount << "." << endl;
cout << "Your new Balance is Kshs. " << Balance << "." << endl << endl;
cout << "Thank you for using this ATM.Welcome back";
cout << endl;
}
}
system("pause");
return 0;
}

Last edited on
closed account (zb0S216C)
A for loop is the best candidate for your main-loop. Here's an example:

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
typedef unsigned short u_short;

u_short correct_pin(1234U);
u_short entered_pin(0U);

bool is_pin_correct(false);

for(u_short attempts(0U); attempts < 3U; ++attempts)
{
    std::cout << "Enter PIN: ";
    std::cin >> entered_pin;

    // If the given PIN isn't equal to the correct PIN, display
    // the remaining attempts and ask again...
    if(entered_pin != correct_pin)
    {
        std::cout << "Attempts remaining: " << (3U - attempts) << std::endl;
        continue;
    }

    // ...otherwise, there's no need to continue since the PIN
    // was correct.
    else if(entered_pin == correct_pin)
    {
        is_pin_correct = true;
        break;
    }
}

if(is_pin_correct)
{
    // Display account information...
}

else
{
    // Handle 3 invalid attempts...
}

Note that the above code is untested.

Wazzak
thanks guys... with your help I've come up with these code, i have added a bit of more checking but somehow it still doesn't work well....could you tell me what i can do to make it work with that flow

here it is:

#include <iostream>

using namespace std;

int main()

{ int pin=4040;
int amount= 50000;
int withdrawal_amount;
int balance;
int pin_trials=3;
int count;

cout<<"\nwelcome to our ATM services!"<<endl;

while (pin_trials>0)
{

bool x;


do{
cout<<"\nplease enter your pin number"<<endl;
cin>>pin;
cout<<endl;



if (pin==4040);
{
cout<<"pin entry successful"<<endl;
pin_trials=-1;
x=true;
}

else

{
cout<<"wrong pin...please try again"<<endl;
pin_trials--;
cin>>pin;
cout<<"you have";
cout<<pin_trials;
cout<<"attempts remaining";
x=false;
}
if (pin_trials==0)
{
cout<<"that was the last trial! please contact us at our offices.";
return 0;
}
}
while (x!=true);

if (pin_trials=-1 && withdrawal_amount<amount)

{cout<<"\nenter the amount you wish to withdraw"<<endl;
cin>>withdrawal_amount;
balance= amount-withdrawal_amount;
cout<<"your balance is"<<balance;
cout<<"\nThank you for using our ATM services!";
}

else
{ cout<<"you cannot withdraw more than you have in your account. please try again";
cin>>withdrawal_amount;
}


}

}




Please wrap you code. with these:

[cOde][\code] but \ should be /

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
#include <iostream>

using namespace std;

int main()
{ 
     int pin = 4040;
     int amount = 50000;
     int withdrawal_amount;
     int balance;
     int pin_trials = 3;
     int count;

     cout << "\nwelcome to our ATM services!" << endl;

     while (pin_trials > 0)
     {
          bool x;


          do{
               cout << "\nplease enter your pin number" << endl;
               cin >> pin;
               cout << endl;

               if (pin==4040);
               {
                    cout << "pin entry successful" << endl;
                    pin_trials = -1;
                    x = true;
               }

               else
               {
                    cout << "wrong pin...please try again" << endl;
                    pin_trials--;
                    cin >> pin;
                    cout << "you have";
                    cout << pin_trials;
                    cout << "attempts remaining";
                    x = false;
                }
               
                if (pin_trials == 0)
                {
                     cout << "that was the last trial! please contact us at our offices.";
                     return 0;
                }
               }while (x != true);

          if (pin_trials=-1 && withdrawal_amount < amount)
          {
               cout << "\nenter the amount you wish to withdraw" << endl;
               cin >> withdrawal_amount;
               balance = amount - withdrawal_amount;
               cout << "your balance is" << balance;
               cout << "\nThank you for using our ATM services!";
           }

           else
           { 
                cout << "you cannot withdraw more than you have in your account. please try again";
                cin >> withdrawal_amount;
            }
     }
}





Nice and organized. One thing I hate is poor formatting.
Last edited on
Topic archived. No new replies allowed.