Written by Sai Sandeep Thota on January 6, 2013 in C++ > Programming

Simple Banking Program using C++

GET ALERTS:

Get our Latest updates delivered to your mailbox!

Today I am going to write a solution for simple banking program in C++. The program uses Classes and objects. Coming to the explanation of the program:

#1. We first defined a class called Bank.
#2. In the class we have defined ac – Account Number as int (integer), reset is defined for Checking balance and float Balance, amount are used for calculating Deposits and Withdrawls. Shorta variable is used for calculating Withdrawl limit.
#3. If you define a static variable inside a class, you must define it outside of class using the format dataype classname::staticvariable-name;. Where :: is scope resolution operator.
#4. We are using a single account number (Here we are using a account number: 1001) here, But we’ll write another program that will use an array with a list of Account Numbers.
#5. If you want to deposit money, You need to enter the account number and amount you would like to deposit, We have used reset++ in the Deposit because it is used for chkbalance() function.
#6. If you would like to withdraw money, You need to enter the account number and amount you would like to withdraw. Here comes the use of our variable shorta. Using an if condition, if the amount entered is greater than the account balance then it will throw an error that Your account is short of some x money.
#7. We are using reset variable for the purpose of clearing any garbage value in Balance variable. If the user checks for Balance without any deposit in the account, he may get some garbage valued output. So if he has deposited some money into the account, reset will be incremented leaving the balance variable intact.

Banking Program in C++

/* Simple Banking Program using C++ */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class bank {
	int ac;
	static reset;
	float balance, amount, shorta;

	public:
	void deposit();
	void withdraw();
	void chkbalance();
	int menu();
};

int bank::reset=0; 

void bank::deposit() {
	cout<<"\nEnter Account Number:";
	cin>>ac;
	if(ac==1001)
	{
		cout<<"\nEnter Amount you want to Deposit:";
		cin>>amount;
		balance=balance+amount;
		cout<<"\n\nMessage: You have successfully deposited Rs."<<amount<<" into your account.\n\nYour Current Balance is:"<<balance<<"\n\n";
		reset++;
	}
	else
	{
		cout<<"You have entered invalid account number";
	}
}

void bank::withdraw() {
	cout<<"Enter Account Number:";
	cin>>ac;
	if(ac==1001)
	{
		cout<<"Enter Amount you want to Withdraw:";
		cin>>amount;
		if(amount>balance)
		{
			shorta=amount-balance;
			cout<<"\n\nYou cannot withdraw Rs."<<amount<<" as your account balance is Rs."<<balance<<"\n\nYou are short of Rs."<<shorta;
		}
		else
		{
			balance=balance-amount;
			cout<<"You have Withdrawn Rs."<<amount<<" Successfully\n\n Your account balance is:"<<balance<<"\n\n";
		}
	}
	else
	{
		cout<<"You have entered invalid account number";
	}
}

void bank::chkbalance() {
	cout<<"Enter Account Number:";
	cin>>ac;
	if(reset==0)
	{
		balance=0;
	}
	if(ac==1001)
	{
		cout<<"Your Account balance is Rs."<<balance<<"\n\n";
	}
	else
	{
		cout<<"Account Doesn't Exist!!";
	}
}


void main()
{
	bank ob;
	int ch;
	clrscr();

	do {
		cout<<"\n\n\n1.Deposit\n2.Withdraw\n3.Balance Enquiry\n4.Exit\n\n";
		ch=ob.menu();
		switch(ch)
		{
		case 1:ob.deposit();
			break;
		case 2:ob.withdraw();
			break;
		case 3:ob.chkbalance();
			break;
		case 4: exit(1);
		default:cout<<"Invalid Choice!!";
		}
	}while(1);
}

int bank::menu()
{
	int ch;
	cout<<"\nEnter your Choice:";
	cin>>ch;
	return ch;
}

Download Source Code

Output:
deposit-banking-program-cpp

withdraw-banking-program-cpp

check-balance-banking-program-cpp

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: