rendered paste body//Program 9
#include <iostream>
#include <cstring>
#include <fstream>
#define SZ 10
using namespace std;
class Account
{
char name[30];
int account_no;
float balance;
public:
Account()
{
name[0] = '\0';
account_no = 0;
balance = 0;
}
void set(int num, char n[], float b = 0)
{
strcpy(name, n);
account_no = num;
balance = b;
}
void display()
{
cout<<"\n\nAccount number : "<<account_no
<<"\nName : "<<name
<<"\nBalance : "<<balance<<endl;
}
void deposit()
{
float b;
cout<<"\nEnter amount to be deposited : ";
cin>>b;
balance += b;
cout<<"\nNew balance :"<<balance<<endl;
}
void withdraw()
{
float b;
cout<<"\nEnter amount to be withdrawn : ";
cin>>b;
if(b >= balance)
cout<<"\nNot enough balance to withdraw from this account.\n";
else
{
balance -= b;
cout<<"\nNew balance :"<<balance<<endl;
}
}
};
int main()
{
Account acc[SZ];
int i, a, acnum = 0, choice = 4;
char name[30], reply = 'y';
do
{
cout<<"\nMenu : "
<<"\n1.Create a new account."
<<"\n2.Work with an existing account."
<<"\n3.Display details of an account."
<<"\n4.Quit"
<<"\nYour choice here : ";
while(!(cin>>choice) || choice < 0 || choice > 4)
cout<<"\nInvalid choice. Enter again : ";
switch(choice)
{
case 1: if(acnum >= SZ)
{
cout<<"\nCannot store more accounts.";
break;
}
cout<<"\nEnter name :";
cin.clear();
cin.ignore(1, '\n');
cin.getline(name,30);
acc[acnum].set(acnum + 1000, name);
acc[acnum].deposit();
cout<<"\nAccount created successfully.\nAccount details : ";
acc[acnum].display();
acnum++;
break;
case 2: cout<<"\nEnter account number : ";
cin>>a;
if(a-1000 >= acnum || a < 1000)
{
cout<<"\nSuch an account does not exist.";
break;
}
cout<<"\nYour account is : ";
acc[a-1000].display();
cout<<"\nEnter \"1\" to deposit or any other number to withdraw.";
cin>>i;
if(i == 1)
acc[a-1000].deposit();
else
acc[a-1000].withdraw();
break;
case 3: cout<<"\nEnter account number : ";
cin>>a;
if(a-1000 >= acnum || a < 1000)
{
cout<<"\nSuch an account does not exist.";
break;
}
cout<<"\nYour account is : ";
acc[a-1000].display();
break;
case 4: cout<<"\nWriting saved data to file \"ACCOUNTS.TXT\"";
fstream file;
file.open("ACCOUNTS", ios::in | ios:: out);
for(i = 0; i < acnum; i++)
file.write((char*)&acc[i], sizeof(acc[i]));
cout<<"\nPress ENTER to view contents of ACCOUNTS.TXT.";
getchar();
getchar();
file.seekg(0);
Account a;
for(i = 0; i < acnum; i++)
{
file.read((char*)&a, sizeof(a));
a.display();
cout<<"\n\n";
}
file.close();
}
}while(choice != 4);
system("pause");
return 0;
}