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;
}
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.
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;
}