C++ Project – Bank Management System using File Part – 1
Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
// Bank Application Project with File Storage
#include<iostream>
#include<fstream> // For file handling
using namespace std;
class Bank
{
private:
int accno;
string name;
int balance;
public:
Bank()
{
balance = 0;
}
void create_account();
void deposit_amount();
void withd_amount();
void display();
void writeToFile(); // New method to save data to file
};
void Bank::create_account()
{
cout << "\nEnter account no: ";
cin >> accno;
cout << "Enter name: ";
cin >> name;
writeToFile(); // save to file immediately
}
void Bank::deposit_amount()
{
int amt;
cout << "\nEnter amount for deposit: ";
cin >> amt;
balance += amt;
cout << "\nCurrent balance is: " << balance;
writeToFile();
}
void Bank::withd_amount()
{
int amt;
cout << "\nEnter amount for withdrawal: ";
cin >> amt;
if (amt > balance)
cout << "\nInsufficient balance";
else {
balance -= amt;
cout << "\nCurrent balance is: " << balance;
writeToFile();
}
}
void Bank::display()
{
cout << "\nAccount No: " << accno;
cout << "\nName: " << name;
cout << "\nCurrent balance is: " << balance;
}
// Save account details to file
void Bank::writeToFile()
{
ofstream fout("G://cppdata//BankData.txt", ios::app); // append mode
if (fout.is_open())
{
fout << "Account No: " << accno << "\n";
fout << "Name: " << name << "\n";
fout << "Balance: " << balance << "\n";
fout << "--------------------------\n";
fout.close();
}
else
{
cout << "\n Error opening file for writing.";
}
}
int main()
{
system("cls");
int choice;
Bank B1;
do {
cout << "\n--------- SBI Bank Application ---------";
cout << "\n1. Create Account";
cout << "\n2. Deposit Amount";
cout << "\n3. Withdrawal Amount";
cout << "\n4. Display";
cout << "\n5. Exit";
cout << "\n----------------------------------------";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice)
{
case 1: B1.create_account(); break;
case 2: B1.deposit_amount(); break;
case 3: B1.withd_amount(); break;
case 4: B1.display(); break;
case 5: cout << "\nExiting program...\n"; break;
default: cout << "\nInvalid choice"; break;
}
} while (choice != 5);
return 0;
}
Did we exceed your expectations?
If Yes, share your valuable feedback on Google

