A customer opens an account at a bank with an initial amount. The account number, name, and balance of the customer are generated. Write a programme to input data for 5 customers and write into a FILE. Later, display the data of all customers by reading from the FILE
#include <iostream>
#include <string.h>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//Define a strcut for saving data
struct Bank
{
unsigned ID;//ID accaunt bank
char name[35];//name
double balance;//balance
Bank()
{
}
Bank(unsigned id,const char*nm,double b)
{
this->ID=id;
strcpy(name,nm);
this->balance=b;
}
void printInformation()
{
cout<<"|"<<setw(8)<<ID<<"|"<<setw(30)<<name<<"|"<<setw(9)<<balance<<"|\n";
}
};
//function save data to file
void SaveAs(const char*fname,Bank* bn,int size)
{
ofstream out(fname,ios::app);
for(int i=0;i<size;i++)
{
out<<setw(8)<<bn[i].ID<<setw(30)<<bn[i].name<<setw(9)<<bn[i].balance<<"\n";
}
out.close();
}
//OutputData
void ReadData(const char*fname)
{
ifstream inp(fname);
string line;
while(getline(inp,line))
{
cout<<line<<endl;
}
inp.close();
}
int main()
{
int n=5;
Bank *bn=new Bank[5];
for(int i=0;i<5;i++)
{
int ID;
char nm[35];
double balance;
cout<<"ID:";
cin>>ID;
cout<<"Name: ";
cin>>nm;
cout<<"balance: ";
cin>>balance;
bn[i].balance=balance;
bn[i].ID=ID;
strcpy(bn[i].name,nm);
}
SaveAs("output.txt",bn,n);
ReadData("output.txt");
delete[] bn;
return 0;
}
Comments
Leave a comment