Thread: Bank Program - Validing a PIN #

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Bank Program - Validing a PIN #

    I'm working on a bank account program and I need some help with the part that validates the PIN #. I know it will be a FOR statement of some sort, but could use a little help figuring out how to set this up. I need a variable, something to set it equal to and what else??? Here's my code - I got it running pretty well so far (just a few bugs I need to go back and fix) but there's nothing about validating a PIN yet...I'll want to put it in the int getPin part, right?

    insert
    Code:
    //Final Project 
    
    #include <iostream>
    #include <cstdlib>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    class userAccount
    {
    private:
    	string userName;
    	int pin;
    	string firstName;
    	string lastName;
    	int account;
    	double balance;
    
    public:
    	
    	userAccount()
    	{
    		//don't initialize because no parameters have been passed
    	}
    	
    	userAccount(string u, string f, string l, int a, int p, double b)
    	{
    		userName=u;
    		pin=p;
    		firstName=f;
    		lastName=l;
    		balance=b;
    		account=a;
    	}
    
    	string getUser()
    	{
    		return userName;
    	}
    	
    	int getPin()
    	{
    		return pin;
    	}
    
    	int getAccount()
    	{
    		return account;
    	}	
    	string getFirstName()
    	{
    		return firstName;
    	}
    
    	string getLastName()
    	{
    		return lastName;
    	}
    
    	double getBalance()
    	{
    		return balance;
    	}
    
    	void printBalance()
    	{
    		cout<<"Your current balance is $ "<<balance<<endl;
    	}
    
    	void setPin()
    	{
    		cout<<"Enter your new PIN:  "<<endl;
    		cin>>pin;
    	}
    
    	void printData()
    	{
    		cout<<"Enter Username:  "<<endl;
    		cin>>userName;
    		cout<<"Enter PIN "<<endl;
    		cin>>pin;
    		cout<<"Enter First Name: "<<endl;
    		cin>>firstName;
    		cout<<"Enter Last Name; "<<endl;
    		cin>>lastName;
    		cout<<"Enter Account Number: "<<endl;
    		cin>>account;
    		cout<<"Your current balance is $ "<<endl;
    		cin>>balance;
    	}
    
    	void addDeposit()
    	{
    		double deposit;
    
    		cout<<"Enter your deposit amount: $ "<<endl;
    		cin>>deposit;
    		balance = balance + deposit;
    		cout<<"Your new balance is $ "<<balance;
    		
    
    	}
        void getWithdrawal()
        {
    		double withdrawal;
    		
    	cout<<"Enter your withdrawal amount:  $ "<<endl;
        cin>>withdrawal;
    
    	while(withdrawal > balance || withdrawal < 0)
    	{
    		cout<<"DENIED!  You have entered an inadequate amount."<<endl;
    		cout<<"Please enter your withdrawal amount:  $ "<<endl;
    		cin>>withdrawal;
        }
    
    	balance = balance - withdrawal;
    	cout<<"Your updated balance is now $ "<<balance;
    
    }
    };
    
    int makeChoice()
    {
    	int choice;
    
    	cout<<"1.)  View Account Info"<<endl;
    	cout<<"2.)  Withdraw"<<endl;
    	cout<<"3.)  Deposit"<<endl;
    	cout<<"4.)  Change PIN"<<endl;
    	cout<<"5.)  Quit"<<endl;
    	cout<<"What action do you want to take?"<<endl;
    	cin>>choice;
    	return choice;
    
    	}
    
    	int main()
    {
    	//set up for reading from file
    	ifstream accountData;
    	accountData.open("Account.txt");
    
    
    	string tempUserName;
    	string tempFirstName;
    	string tempLastName;
    	int tempAccount;
    	int tempPin;
    	double tempBalance;
    	int choice;
    	int i;
    	int totalUsers = 0; //keeps track of how many items we have total
    	int activeUser = 0;
    	int activePin = 0; //this is the item we are looking for and will modify
    	userAccount users[100]; //all of our items are kept in this array
    
    	//getting data from file into array
    	while (!accountData.eof())
    	{
    		//get input from file
    		accountData>>tempUserName>>tempAccount>>tempPin>>tempFirstName>>tempLastName>>tempBalance;
    		//use constructor to create a temp object
    		//cout<<tempUserName<<endl;
    		userAccount tempUser(tempUserName, tempFirstName, tempLastName, tempAccount, tempPin, tempBalance);
    		//assign temp object to a spot in our array
    		users[totalUsers] = tempUser;
    		//increment count for the next item
    		totalUsers++;
    	}
    	
    	//ask user for ID loop
    	do
    	{
    
    		cout<<"Enter your user name: "<<endl;
    		cin>>tempUserName;
    		cout<<"Enter your PIN:  "<<endl;
    		cin>>tempPin;
    		activeUser = 0;
    
    	
    		//keeps look through array to find our item
    		while(tempPin != users[activeUser].getPin() && tempUserName != users[activeUser].getUser())
    		
    		{
    			activeUser++;
         	}
    	
    	}
    	while(activeUser == totalUsers);
    	{
    	cout<<"Thank You for Visiting Bank 203"<<endl;
    	users[activeUser].printBalance();
    	choice = makeChoice();
    	}
    	while (choice != 5)
    	{
    		if (choice ==1)
    		{
    			users[activeUser].printData();
    		}
    		else if (choice == 2)
    		{
    			users[activeUser].getWithdrawal();
    		}
    		else if (choice == 3)
    		{
    			users[activeUser].addDeposit();
    		}
    		else if (choice == 4)
    		{
    			users[activeUser].setPin();
    		}
    		choice = makeChoice();
    	}
    
    	ofstream accountData2;
    	accountData2.open("Account.txt");
    
    	for ( i =0; i < totalUsers; i++)
    	{
    		accountData2<<users[i].getUser()<<users[i].getFirstName()<<users[i].getLastName()<<users[i].getAccount()<<users[i].getPin()<<users[i].getBalance()<<endl;
    	}
    	
    	system("pause");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    something like this?

    Code:
    string pin;
    cin >> pin;
    bool is_valid = true;
    for(int i=0;i<pin.length();++i)
    {
       if( !isdigit(pin[i]) )
       {
          is_valid = false;
          break; 
       }
    }
    
    if(is_valid && pin.length() == 4)
       { // pin is valid  }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. bank account program
    By JJH35 in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2006, 06:05 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread