# [resolved] bank account C++



## karen_CSE

*bank account C++*

Hi, I have a big project (#1). this is its direction:
Project I

Software for a Bank

Write a C++ program for the following task:
a) Should allow 5 users with user name & password [ (Ohlone1, Ohlone1pw), (Ohlone2, Ohlone2pw),( Ohlone3, Ohlone3pw), (Ohlone4, Ohlone4pw), (Ohlone5, Ohlone5pw)]

b) Display a welcome / greeting .

c) Menu driven program to allow ‘View Balance’, ‘Deposit’, ‘Withdraw’, ‘Transfer’ and ‘Exit’. Start with a beginning balance of 1000.00 in checking and 1500.00 in savings.

These are the general specifications, use your creativity to make program user friendly.


So far I've laid out the general scheme in the following code. But I have no idea how to do the depositing, withdrawing, or the transfering...worse yet...How can you show these amounts on the "view Balance"?
Do I have to repeat this for each and every user? Is there a simpler way to simplify this process?




Code:


//Term Project #1 - 100 points
//SOFTWARE FOR A BANK 
//Write a C++ program for the following task using modular approach.
//a)    Should allow  5 users with user name & password :
//[ (Ohlone1, Ohlone1pw), (Ohlone2, Ohlone2pw),( Ohlone3, Ohlone3pw), (Ohlone4, Ohlone4pw), (Ohlone5, Ohlone5pw)]
//b)    Display a welcome / greeting .
//c)    Menu driven program to allow: 
//‘View Balance’, 
//‘Deposit’, 
//‘Withdraw’, 
//‘Transfer’ 
//and ‘Exit’. 
//Start with a beginning balance of 1000.00 in checking and 1500.00 in savings.

#include <iostream.h>
#include <string.h>

//PROTOTYPE;
void Signin();
void Menu1();
void ViewBalance1();
void Ohlone1_balance();
void Ohlone1_Deposit();
void Ohlone1_Withdraw();
void Ohlone1_Transfer();
void Ohlone1_Checking();
void Ohlone1_Saving();


void main (void)
{

	cout << "WELCOME TO PROGRAMMING BANK!\n\n";
	cout << "~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~\n\n";
	Signin ();
	Menu1 ();
}

void Signin ()
{
	char username[31], PW[20];

	cout << "Please enter your username: \n";
	cin.width(32);
	cin >> username;
	do 
	{
		//User 1
		if (strcmp(username, "Ohlone1") ==0)
		{
			cout << "Please enter your password: \n";
			cin >> PW;
			for (int i = 0; i < PW; i++)
			{
				cout << "*";     //how come this doesn’t work?
			}
			if (strcmp(PW, "Ohlone1pw") == 0)
				cout << "\n\n\tYou are logged in!\n\t";
			else 
			{
				cout << "Your password was entered incorrectly.\n";
				cout << "Please enter your password again.\n";
				cin >> PW;
				if (strcmp(PW, "Ohlone1pw") == 0)
					cout << "\n\n\tYou are logged in!\n\n";
				else 
				{
					cout << "I'm sorry you're having trouble.\n";
					break;
				}
			}
			return;
		}
		//User 2
		else if (strcmp(username, "Ohlone2") ==0)
		{
			cout << "Please enter your password: \n";
			cin >> PW;
			if (strcmp(PW, "Ohlone2pw") == 0)
				cout << "\n\n\tYou are logged in!\n\n";
			else 
			{
				cout << "Your password was entered incorrectly.\n";
				cout << "Please enter your password again.\n";
				cin >> PW;
				if (strcmp(PW, "Ohlone2pw") == 0)
					cout << "\n\n\tYou are logged in!\n\n";
				else
				{
					cout << "I'm sorry you're having trouble.\n";
					break;
				}
			}
			return;
		}
		//User 3
		else if (strcmp(username, "Ohlone3") == 0)
		{
			cout << "Please enter your password: \n";
			cin >> PW;
			if (strcmp(PW, "Ohlone3pw") == 0)
				cout << "\n\n\tYou are logged in!\n\n";
			else 
			{
				cout << "Your password was entered incorrectly.\n";
				cout << "Please enter your password again.\n";
				cin >> PW;
				if (strcmp(PW, "Ohlone3pw") == 0)
					cout << "\n\n\tYou are logged in!\n\n";
				else
				{
					cout << "I'm sorry you're having trouble.\n";
					break;
				}
			}
			return;
		}
		//User 4
		else if (strcmp(username, "Ohlone4") == 0)
		{
			cout << "Please enter your password: \n";
			cin >> PW;
			if (strcmp(PW, "Ohlone4pw") == 0)
				cout << "\n\n\tYou are logged in!\n\n";
			else 
			{
				cout << "Your password was entered incorrectly.\n";
				cout << "Please enter your password again.\n";
				cin >> PW;
				if (strcmp(PW, "Ohlone4pw") == 0)
					cout << "\n\n\tYou are logged in!\n";
				else 
				{
					cout << "I'm sorry you're having trouble.\n";
					break;
				}
			}
			return;
		}
		//User 5
		else if (strcmp(username, "Ohlone5") == 0)
		{
			cout << "Please enter your password: \n";
			cin >> PW;
			if (strcmp(PW, "Ohlone5pw") == 0)
				cout << "\n\n\tYou are logged in!\n\n";
			else 
			{
				cout << "Your password was entered incorrectly.\n";
				cout << "Please enter your password again.\n";
				cin >> PW;
				if (strcmp(PW, "Ohlone5pw") == 0)
					cout << "\n\n\tYou are logged in!\n\n";
				else
				{
					cout << "I'm sorry you're having trouble.\n";
					break;
				}
			}
			return;
		} 
	}while (strcmp(username, "Ohlone1") == 0 || (username, "Ohlone2") == 0 || 
		(username, "Ohlone3") == 0 || (username, "Ohlone4") ==0 || (username, "Ohlone5") == 0);
     
	if (strcmp(username, "Ohlone1") != 0 || (username, "Ohlone2") != 0 || 
		(username, "Ohlone3") != 0 || (username, "Ohlone4") !=0 || (username, "Ohlone5") != 0)
	{
		cout << "your enter your username incorrectly.\n";
		cout << "Please try again.\n";
	}
	return;
	}
void Menu1 ()
{
	int choice;

	cout << "\nWHAT WOULD YOU LIKE TO DO?\n";
	cout << "\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n";
	cout << "\t1. View Your Balance.\n";
	cout << "\t2. Deposit.\n";
	cout << "\t3. Withdraw.\n";
	cout << "\t4. Transfer.\n";
	cout << "\t5. Exit.\n\n";
	cout << "Please enter your choice: \n";
	cin >> choice; 
	if (choice ==1)
	{
		Ohlone1_balance();
	}
	else if (choice ==2)
	{
		Ohlone1_Deposit();
	}
	else if (choice ==3)
	{
		Ohlone1_Withdraw();
	}
	else if (choice ==4)
	{
		Ohlone1_Transfer();
	}
	else if (choice ==5)
	{
		cout << "You have chosen to exit the menu.\n";
		cout << "Thank you for visiting Programming Bank.\n";
		return;
	}
}

void Ohlone1_balance ()
{
	//cout << "Your checking account has : " << << endl;
	//cout << "Your saving account has : " <<  << endl;
}

void Ohlone1_Checking (float *amount)
{
	//amount = 1000;

}

void Ohlone1_Saving (float *amount)
{
	//amount = 1500;
}

void Ohlone1_Deposit ()
{
	int input;
	float deposit; 

	cout << "Which account would you like to deposit to?\n";
	cout << "1. Checking Account\n";
	cout << "2. Saving Account\n";
	cin  >> input;
	if (input = 1)
	{
		cout << "How much do you want to deposit to the Checking Account?\n";
		cin >> deposit;
	}
	if (input =2)
	{
		cout << "How much do you want to deposit to the Saving Account?\n";
		cin >> deposit;
	}
}

void Ohlone1_Transfer ()
{
	int input;

	cout << "Would you like to transfer..\n\n";
	cout << "1. From CHECKING to SAVING\n";
	cout << "\nOR\n\n";
	cout << "2. From SAVING to CHECKING\n";
	cin >> input;
	if (input = 1)
	{
		// C_to_S ();
	}
	if (input = 2)
	{
		// S_to_C ();
	}
}

void Ohlone1_Withdraw ()
{
	int input;
	float withdraw;

	cout << "From which account would you like to withdraw?\n";
	cout << "1. Checking\n";
	cout << "2. Saving\n";
	cin >> input;
	if (input = 1)
	{
		cout << "How much would you like to withdraw from the Checking account?\n";
		cin >> withdraw;
		//what to do here? 
	}
	if (input = 2)
	{
		cout << "How much would you like to withdraw from the Saving account?\n";
		cin >> withdraw;
		//what to do here?
	}
}

I know it's really long. But can you guys help please?

thanx in advance,
Karen


----------



## LoneWolf071

Have IT To You In The Morning Boss... LOL, How Did The Other One Work Out?


----------



## karen_CSE

LoneWolf071 said:


> Have IT To You In The Morning Boss... LOL, How Did The Other One Work Out?


yeah, it did. Thanks a bunch for your help!

I have a question though. Is it Ok if I post my just-solved program here? I mean, it is a simple program (yeah, right) to a lot of experienced programmers, but for beginners like me, it's quite a brain-racker. Personally, I find an example to be quite helpful because it shows me the proper way to code (which I'm still learning). So posting the solution online wouldn't be considered...anything bad (cough *cheating* cough), would it? 

Karen


----------



## LoneWolf071

I Have No Problems With That.... Although try not to take up much space... Make it a continutal Thread...


----------



## LoneWolf071

Code:


#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

using namespace std;

struct customer{
	float savings;
	float checking;
	char username[9];
} ohlone[5];

//PROTOTYPE;
int Signin(void);
void Menu1(int );
void ViewBalance1(int);
void ohlone_balance(int );
void ohlone_deposit(int );
void ohlone_withdraw(int );
void ohlone_transfer(int );
void ohlone_checking_deposit(int );
void ohlone_checking_withdraw(int );
void ohlone_savings_deposit(int );
void ohlone_savings_withdraw(int );


int main()
{
	int i,users;
	
	for(i = 0; i < 5; i++)
	{
		ohlone[i].savings = 1500.00;
		ohlone[i].checking = 1000.00;
	}
	cout << "WELCOME TO PROGRAMMING BANK!\n\n";
	cout << "~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~\n\n";
	users = Signin();
	Menu1(users);
	return 0;
}

int Signin()
{
	char username[31], PW[20];
	int tries, user;

	cout << "Please enter your username: \n";
//	cin.width(32);
	cin >> username;
	if (strcmp(username, "ohlone1") == 0 || strcmp(username, "ohlone2") == 0 || strcmp(username, "ohlone3") == 0 || strcmp(username, "ohlone4") ==0 || strcmp(username, "ohlone5") == 0)
	{
		//User 1
		if (strcmp(username, "ohlone1") ==0)
		{
			tries = 0;
			cout << "Please enter your password: \n";
			cin >> PW;

			if (strcmp(PW, "ohlone1pw") == 0)
			{
				cout << "\n\n\tYou are logged in!\n\t";
				tries = 3;
			}

			while(strcmp(PW, "ohlone1pw") != 0 && tries != 3)
			{
				tries++;
				cout << "Your password was entered incorrectly.(It's CaSe SeNsAtIvE)\n";
				cout << "Please enter your password again.\n";
				cin >> PW;	
			}
			strcpy(ohlone[0].username, username);
			user = 0;
		}
		//User 2
		else if (strcmp(username, "ohlone2") ==0)
		{
			tries=0;
			cout << "Please enter your password: \n";
			cin >> PW;
			
			if (strcmp(PW, "ohlone2pw") == 0)
			{
				cout << "\n\n\tYou are logged in!\n\n";
				tries=3;
			}
			while(strcmp(PW, "ohlone2pw") != 0 && tries != 3)
			{
				cout << "Your password was entered incorrectly.(It's CaSe SeNsAtIvE)\n";
				cout << "Please enter your password again.\n";
				cin >> PW;
			}
			strcpy(ohlone[1].username, username);
			user = 1;
		}
		//User 3
		else if (strcmp(username, "ohlone3") == 0)
		{
			tries=0;
			cout << "Please enter your password: \n";
			cin >> PW;
			
			if (strcmp(PW, "ohlone3pw") == 0)
			{
				cout << "\n\n\tYou are logged in!\n\n";
				tries=3;
			}
			while(strcmp(PW, "ohlone3pw") != 0 && tries != 3)
			{
				cout << "Your password was entered incorrectly.(It's CaSe SeNsAtIvE)\n";
				cout << "Please enter your password again.\n";
				cin >> PW;
			}
			strcpy(ohlone[2].username, username);
			user = 2;
		}
		//User 4
		else if (strcmp(username, "ohlone4") == 0)
		{
			tries=0;
			cout << "Please enter your password: \n";
			cin >> PW;
			
			if (strcmp(PW, "ohlone4pw") == 0)
			{
				cout << "\n\n\tYou are logged in!\n\n";
				tries=3;
			}
			while(strcmp(PW, "ohlone4pw") != 0 && tries != 3)
			{
				cout << "Your password was entered incorrectly.(It's CaSe SeNsAtIvE)\n";
				cout << "Please enter your password again.\n";
				cin >> PW;
				
			}
			strcpy(ohlone[3].username, username);
			user = 3;
		}
		//User 5
		else if (strcmp(username, "ohlone5") == 0)
		{
			tries=0;
			cout << "Please enter your password: \n";
			cin >> PW;
			
			if (strcmp(PW, "ohlone5pw") == 0)
			{
				cout << "\n\n\tYou are logged in!\n\n";
				tries=3;
			}
			while(strcmp(PW, "ohlone5pw") != 0 && tries != 3)
			{
				cout << "Your password was entered incorrectly.(It's CaSe SeNsAtIvE)\n";
				cout << "Please enter your password again.\n";
				cin >> PW;
				
			}		
			strcpy(ohlone[4].username, username);
			user = 4;
		} 
	}
     
	else if (strcmp(username, "ohlone1") != 0 && strcmp(username, "ohlone2") != 0 && strcmp(username, "ohlone3") != 0 && strcmp(username, "ohlone4") !=0 && strcmp(username, "ohlone5") != 0)
	{
		cout << "your enter your username incorrectly.\n";
		cout << "Please try again.\n";
		exit(1);
	}
	return user;
}
	//done up to here 07/22/05 8:30 PM
	
void Menu1(int user)
{
	int choice;
	do{
		
		cout << "\nWHAT WOULD YOU LIKE TO DO ?\n";
		cout << "\n*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n\n";
		cout << "\t1. View Your Balance.\n";
		cout << "\t2. Deposit.\n";
		cout << "\t3. Withdraw.\n";
		cout << "\t4. Transfer.\n";
		cout << "\t5. Change Accounts.\n";
		cout << "\t6. Exit.\n\n";
		cout << "Please enter your choice: \n";
		cin >> choice;
		cout << "\n\n" << endl;
		while(choice > 6 && choice < 0)
		{
			cout << "The Number Must Be Between 1 And 5..." << endl;
			cin >> choice;
		}
	
		if (choice ==1)
		{
			ohlone_balance(user);
		}
		else if (choice ==2)
		{
			ohlone_deposit(user);
		}
		else if (choice ==3)
		{
			ohlone_withdraw(user);
		}
		else if (choice ==4)
		{
			ohlone_transfer(user);
		}
		else if (choice == 5)
		{
			user = Signin();
		}
	}while(choice != 6);
		cout << "You have chosen to exit the menu.\n";
		cout << "Thank you for visiting Programming Bank.\n";
	
}

void ohlone_balance (int user)
{
	cout << "\nYour checking account has : " << ohlone[user].checking << endl;
	cout << "Your saving account has : " << ohlone[user].savings << "\n\n" << endl;
}

void ohlone_checking_deposit(int user)
{
	float amount;
	cout << "You Have " << ohlone[user].checking << " In Your Checking Account."<<endl;
	cout << "How Much Would You Like To Deposit Into Your Checking Account?"<<endl;
	cin >> amount;
	ohlone[user].checking += amount;
}

void ohlone_checking_withdraw(int user)
{
	
	float amount;
	cout << "You Have " << ohlone[user].checking << " In Your Checking Account."<<endl;
	cout << "How Much Would You Like To Withdraw Into Your Checking Account?"<<endl;
	cin >> amount;
	while(amount > ohlone[user].checking)
	{
		cout << "You Only Have " << ohlone[user].checking << " In Your Checking Account."<<endl;
		cout << "Enter A Number Lower Then The Amount In You Checking Account." << endl;
		cin >> amount;
	}
	ohlone[user].checking -= amount;
}

void ohlone_savings_withdraw(int user)
{
	float amount;
	cout << "You Have " << ohlone[user].savings << " In Your Savings Account."<<endl;
	cout << "How Much Would You Like To Withdraw Into Your Savings Account?"<<endl;
	cin >> amount;
	while(amount > ohlone[user].savings)
	{
		cout << "You Only Have " << ohlone[user].savings << " In Your Savings Account."<<endl;
		cout << "Enter A Number Lower Then The Amount In You Savings Account." << endl;
		cin >> amount;
	}
	ohlone[user].savings -= amount;
}

void ohlone_savings_deposit(int user)
{
	float amount;
	cout << "You Have " << ohlone[user].savings << " In Your Savings Account."<<endl;
	cout << "How Much Would You Like To Deposit Into Your Savings Account?"<<endl;
	cin >> amount;
	ohlone[user].savings += amount;
}

void ohlone_deposit (int user)
{
	int input;
	float deposit; 

	cout << "Which account would you like to deposit to?\n";
	cout << "1. Checking Account\n";
	cout << "2. Saving Account\n";
	cin  >> input;
	while( input > 2 && input < 1)
	{
		cout << "1. Or 2." << endl;
		cin >> input;
	} 
	if (input == 1)
	{
		ohlone_checking_deposit(user);
	}
	if (input == 2)
	{
		ohlone_savings_deposit(user);
	}
}

void ohlone_transfer (int user)
{
	int input;
	float amount;

	cout << "Would you like to transfer..\n\n";
	cout << "1. From CHECKING to SAVING\n";
	cout << "\nOR\n\n";
	cout << "2. From SAVING to CHECKING\n";
	cin >> input;
	if (input == 1)
	{
		cout << "How Much Would You Like To Transfer To Your Savings?"<<endl;
		cin >> amount;
		while (amount > ohlone[user].checking)
		{
			cout << "You Don't Have That Much In Your Checking Account, Enter An Amount Again."<<endl;
			cin >> amount;
		}
		ohlone[user].checking -= amount;
		ohlone[user].savings += amount;
	}
	if (input == 2)
	{
		cout << "How Much Would You Like To Transfer To Your Checking?"<<endl;
		cin >> amount;
		while (amount > ohlone[user].savings)
		{
			cout << "You Don't Have That Much In Your Savings Account, Enter An Amount Again."<<endl;
			cin >> amount;
		}
		ohlone[user].checking += amount;
		ohlone[user].savings -= amount;
	}
}

void ohlone_withdraw (int user)
{
	int input;
	float withdraw;

	cout << "From which account would you like to withdraw?\n";
	cout << "1. Checking\n";
	cout << "2. Saving\n";
	cin >> input;
	if (input == 1)
	{
		ohlone_checking_withdraw(user);
	}
	if (input == 2)
	{
		ohlone_savings_withdraw(user);
	}
}

Done... Tested And Proved... Does EVERYTHING... Withdraw And Deposit to all accounts, and you can even change user accounts during the session... You Can Thank Me When Ever You Want... JK... Although I Didn't Comment Any Where In There, so If you Need to Know Anything, PM or IM me Or Just Post it here...


----------



## karen_CSE

Lonewolf, this is just....wow! I don't know what to say! "thank you" just doesn't seem to enough to express my gratitude to you right now. And I'm not saying it to buff you up or anything, but it's the truth. I can't just believe you spent time helping me like this. It's reallly amazing. just curious... how long did it take you? 'Cause I know for a fact, it would have taken me forever. 

I'll go over everything and see how you did it. I'll let you know if I don't understand anything.

Like I said, it's not enough But THANK YOU x 100!

Karen


----------



## LoneWolf071

no problem... it was quite simply, although i couldn't get the password masked. i worked off and on.. it took me about 2 hours?


----------



## karen_CSE

Hi, 
I’ve gone over your code (which I’ve tried to rewrite by myself and it did take a lot more than 2 hours, even though I did peek when I got stuck) and there are a few points I’m not really clear on. 
1)	at the end of each signin, why do we have to put user =0, user =1, user =2…
2)	at the end if the Signin () function, why was it necessary to put exit(1);
3)	in the Menu1 () function, at the end of the do-while loop, you put “while (choice !=5” . I thought it should be like this: “while(choice ==1 || choice ==2 || choice ==3 || choice ==4 || choice ==5 || choice == 6)
I understood the rest of the code, I just changed this one part: instead of putting 
cout << "You have chosen to exit the menu.\n";
cout << "Thank you for visiting Programming Bank.\n";
outside of the do-while loop, I include it in the “default else” of the if/else if statement and added a “break statement” after it.

Oh, It's ok if you can't get the password masked. you've done enough already. I wouldn't know how. that's too advance for me.

I know I’ve said it already, but thanks again!

Karen


----------



## LoneWolf071

Well To Get It masked, You Would Need A Seperate Library, And That's A Lot More work Then Needed... And All Your Question ...

1. So That the struct would be personalised to each account user... so if it's user ohlone1, the struct is ohlone[0] for just his account...

2. That Not At The End, That If The Person Doesn't Have A Valid Signin...

3. Woops... It Should Be While (choice != 6) ... Gotta Fix That... That's There, Because As The Menu States, If They Choose 6, They Want To Exit... I'll Fix That... OK, Look At The Prevous Post Again... I Fixed it... Also, I Know This Is nothing You Need, But There Is A buffer overflow problem, but we can worry about then when the time is right... :wink:


----------



## ricer333

*exit(1)*

Karen,
Just so you know for the future, and exit(1) statement is a way to terminate gracefully instead of just hanging. exit(0) means it worked properly. It's good coding convention from past days (not that I would know, relatively young in the scheme of programming languages) but compilers tend to anticipate coders intentions. But it's still nice. 

So:
exit(1) exit from an error or bad state
exit(0) exit from a good state/ completed what should have been completed.


----------



## LoneWolf071

Yes... I Just Use exit(1) as a force of habit, but i have seen no noticiable difference, but i know that if it's exit(1), the computer writes a log...


----------



## karen_CSE

thanks for the tips, guys! :grin: 
Now I'm off to more programming HW! WAAHHH!!!!! :dead: 

Karen


----------



## deardearest

HELP....................

Hi friends, I m trying do solve this problem but I could not , can anyone help me to do this.


Create a SavingAccount class. Use a static data member to contain the annualInterestRate for each of the savers. Each member of the class contains a private data member savingBalance indicating the amount the save currently has on deposit. Provide a calculateMonthlyInterest member function that calculates the monthly interest by multiplying the balance by annualInterstRate divided by 12; this interest should be added to savingsBalance. Provide a static member function modifyInterestRate that sets the static annualInterestRate to a new value. Write a driver program to test class SavingsAccount. Instantiate three different savingsAccount objects, saver1, saver2 and saver3, with balance of $2000.00, $3000.00 and $8000.00, respectively. Set annualInterestRate to 3%. Then calculate the monthly interest, and print the new balances for each savers. Then set the annualInterestRate to 4%, and calculate the next month’s interest and print the new balances for each of the saver.


----------

