Answer to Question #333432 in C++ for aaaa

Question #333432

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


1
Expert's answer
2022-04-25T13:37:14-0400






#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS