Preview:
#include<iostream>
#include<fstream>
using namespace std;

class Bank_Customer
{
protected:
	char name[40];
	int age;
	int balance;
	char gender;
	char phone[15];
	char address[50];
public:
	void getdata(void) {
		cout << "Enter name:"; cin >> name;
		cout << "Enter age:"; cin >> age;
		cout << "Enter balance:"; cin >> balance;
		cout << "Enter gender(m/f):"; cin >> gender;
		cout << "Enter phone number:"; cin >> phone;
		cout << "Enter Address:"; cin >> address;
	}
	void showdata(void) {
		cout << "\nName:" << name;
		cout << "\nAge:" << age;
		cout << "\nBalance:" << balance;
		cout << "\nGender:" << gender;
		cout << "\nPhone number:" << phone;
		cout << "\nAddress:" << address;
		cout << endl;
	}
	void CreateFile(void){
		fstream file;
		file.open("Customer.txt", ios::app | ios::out | ios::binary);
		if (!file) {
			cerr << "error:input file cannot be opened.\n";
			exit(1);
		}
		else if(file)
		{
			cout << "file already exists and ready for input";
		}
		else
		{
			cout << "File Created Successfuly" << endl << endl;
		}
		file.close();
		
	}
	void AddRecord() {
		ofstream outfile;
		outfile.open("Customer.txt", ios::app | ios::out | ios::binary);
		Bank_Customer c;
		char choice;
		do {
			cout << "\nEnter person's data:" << endl;
			c.getdata();
			outfile.write((char*)&c, sizeof(c));
			cout << "do you want to add another record?(y/n):";
			cin >> choice;

		} while (choice=='y');
		outfile.close();
	}
	void DisplayAllRecords() {
		Bank_Customer cc;
		fstream file;
		file.open("Customer.txt", ios::in | ios::binary);
		if (!file)
		{
			cerr << "Error:input file cannot be openend\n";
			exit(1);
		}
		file.seekg(0);
		file.read((char*)&cc, sizeof(cc));
		while (!file.eof())
		{
			cout << "\nCustomer:";
			cc.showdata();
			file.read((char*)&cc, sizeof(cc));
		}
		cout << endl;
	}
};
int main(void)
{
	cout << "_____Welcome_to__Bank_System_____" << endl;
	cout << "Choose_from_list:" << endl;
	cout << "1-Create-file" << endl;
	cout << "2-Add-record(add customer)" << endl;
	cout << "3-Display all records(Display all customers)" << endl;
	cout << "4-Exit" << endl;
	int ans;
	cin >> ans;
	Bank_Customer cust;
	switch (ans)
	{
	case 1:
		cust.CreateFile();
		cust.AddRecord();
		break;
	case 2:
		cust.AddRecord();
		break;
	case 3:
		cust.DisplayAllRecords();
		break;
	case 4:
		exit(1);
	default:
		cout << "Error";
		exit(1);
		break;
	}
	system("pause");
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter