Accounting Project Source Code

Functions.h

#include iostream
#include vector
#include string
#include fstream//for reading/writing to file
#include sstream

using namespace std;

string itos(int i)	// convert int to string
{
	stringstream s;
	s << i;
	return s.str();
}

string ftos(float i)	// convert float to string
{
	stringstream s;
	s << i;
	return s.str();
}

template < typename T >
void readfile(vector< T >& v)//read from file to vector
{
	string line;
	ifstream infile ("accountData.txt", std::ios_base::in);

	while (getline(infile, line, '\n'))//load file to vector
	{
		v.push_back (line);
	}
	infile.close();//close file
}

template < typename T >
void exitprog(vector< T >& v)//output to file from vector
{
	//clear and output vector contents to file
	int p = v.size();
	ofstream outfile ("accountData.txt", std::ios::in | std::ios::trunc);
	for(int j=0; j
void newaccount(vector< T > & v)//new account creation
{
	string toint, tostring; //conversion strings
	string fullname, password;//user input strings
	int p,q;
	int acct;

	toint = v[0];//get base acct number
	acct = atoi(toint.c_str());//convert base acct number to int
	tostring = itos(acct);

	v.push_back(tostring);//account number

	cout << "Input the Account Holders Name: ";//account name
	getline(cin, fullname);
	v.push_back(fullname);

	cout << "Please choose a Password: ";//account pin
	getline(cin, password);
	v.push_back(password);

	v.push_back(v[4]);//set admin pin
	v.push_back("N");//frozen=No
	v.push_back("0");//account balance

	acct++;//increments account#
	tostring = itos(acct);//convert acct to string
	v[0] = tostring;//write new acct to begining of vector

	//output account info
	p = v.size();
	q = p-6;
	cout <<"\nThank you " << v[q+1] <<". Your new account info is as follows: " << endl;
	cout <<"Your Account Number is: " << v[q] << endl;
	cout <<"Your Pin Number is: " << v[q+2] << endl;
	cout <<"Your Spending Account has a balance of $" << v[q+5] << endl;
}

template < typename T>
string validate(vector< T> & v)//cycle through account number and pin compares
{
	string acctNum, pin;
	int success = 0;

	do
	{
		cout << "Please input your account number: ";
		cin >> acctNum;
	
		for(int i=0; i < v.size(); i++)
		{
			if (acctNum == v[i])
			{
				//input and search for pin to matching account
				cout <<"Account Found. Please input your PIN: ";
				cin >> pin;
				for(int i=0; i < v.size(); i++)
				{
					//pin and account must match
					if (pin == v[i] && acctNum == v[i-2])
					{
						cout <<"\nPIN Accepted.  Welcome " << 
						v[i-1] <<"!\n" << endl;
						success = 1;
						return acctNum;
						break;//exits pin search
					}
					//master account pin and account must match
					else if (pin == v[i] && acctNum == v[i-3])
					{
						cout <<"\nPIN Accepted. 
						Welcome Administrator\n" << endl;
						success = 1;
						return acctNum;
						break;//exits pin search
					}
					else if ((i+1)==v.size())
						cout << "Invalid pin. Please try again"<< endl;
				}
				break;//exits account number search
			}
		else if((i+1)==v.size())
			cout << "You did not input a vaild account number" << endl;
	}
	}while(success != 1);
}

//converts balance from string to float
template < typename T >
float convertBalance(vector< T >& v, string acct)
{
	string balance;
	float dblBalance;
	for(int i=0; i < v.size(); i++)
	{
		if (acct == v[i])//convert balance to float variable
		{
			balance = v[i+5];
			dblBalance = atof(balance.c_str());
			break;
		}
	}
	return dblBalance;
	system("cls");
}

//calculates new account balance after deposit
template 
float deposit(vector& v, string accnt, float curbal)
{
	float deposit, newbal;
	string tostring;

	cout <<"Input how much you would like to deposit: ";
	cin >> deposit;
	newbal = curbal+deposit;

	cout << "\nYour new account balance is: $" << newbal << "\n" << endl;
	for(int i=0; i < v.size(); i++)//finds location of balance for this account
	{
		if (accnt == v[i])//converts dlbBalance to string and writes to vector
		{
			tostring = ftos(newbal);
			v[i+5] = tostring;
			break;//exit update deposit loop
		}
	}
	return newbal;
}

//calculates new account balance after withdraw
template 
float withdraw(vector& v, string accnt, float curbal)
{
	float withdraw, newbal;
	string tostring;

	cout <<"Input how much you would like to withdraw: ";
	cin >> withdraw;
	if (curbal < withdraw)
	{
		cout << "\nYou cant withdraw more than you have available\n" << endl;
		return curbal;
	}
	else
	{
		newbal = curbal-withdraw;

		cout <<"\nYour new account balance is: $" << newbal <<"\n" << endl;
		for(int i=0; i < v.size(); i++)//finds location of balance for this account
		{
			if (accnt == v[i])//converts dlbBalance to string and writes to vector
			{
				tostring = ftos(newbal);
				v[i+5] = tostring;
				break;//exit update deposit loop
			}
		}
		return newbal;
	}
}
				

main.cpp

				
//Chris Hill
//Kenneth Perronne
//Cesar Rodil
//Thomas Zook

#include "functions.h"

int main()
{
	vector < string > file;//create vector 'file'
	file.clear();

	readfile(file);//copy file to vector

	float balance; //account balance
	char response, choice, cont;  //user selection answers

	do
	{
		cout << "Is this a new accout? Y/N :";
		cin >> response;
		cin.get();
		cin.sync();

		switch (response) 
		{
			case 'Y'://if creating account gather info
			case 'y':
				newaccount(file);//creates new account info and loads to vector
	
				do//prompt user to see if they want to continue program or exit
				{
					cout <<"\n\nWould you like to (E)xit or 
					(C)ontinue to make a deposit? ";
					cin >> cont;
					cin.get();
					cin.sync();

					if (cont == 'E' || cont == 'e')
					{
						exitprog(file);
						return 0;//end program
					}
					else if (cont == 'C' || cont == 'c')		
						break;
					else
						cout <<"Invalid entry, try again" << endl;
				}while(cont != 'E' || cont != 'e' || 
				cont != 'C' || cont != 'c');
			case 'n':
			case 'N':
				break;
			default:
				cout <<"Invalid entry, try again" << endl;
		}
	}while(response != 'Y' && response != 'y' && response != 'N' && response != 'n');

	string val_accntnum = validate(file);//validate account number and pin to continue
	balance = convertBalance(file, val_accntnum);//get balance for account chosen

	do
	{
		if (balance > 0)
		{
			cout <<"What would you like to do? \nAccount (B)alance, 
			(D)eposit, (W)ithdraw, or (Q)uit: ";
			cin >> choice;
			cin.get();
			cin.sync();
		}
		else //balance <= 0
		{
			cout <<"What would you like to do? \nAccount (B)alance, 
			(D)eposit, or (Q)uit: ";
			cin >> choice;
			cin.get();
			cin.sync();
		}

		switch (choice)
		{
			case 'b':
			case 'B':
				cout <<"\nYour new account balance is: $" << balance << endl;
				break;
			case 'D':
			case 'd':
				balance = deposit(file, val_accntnum, balance);
				break;
			case 'w':
			case 'W':
				balance = withdraw(file, val_accntnum, balance);
				break;
			case 'q':
			case 'Q':
				break;		
			default:
				cout <<"Invalid input, please try again" << endl;
		}
	}while(choice != 'q' && choice != 'Q');

	exitprog(file);

	return 0;
}