In: Computer Science
In this assignment, you are expected to rewrite the program from assignment7 using structs. That is, instead of using multiple arrays (one for each field, you need to create a single array of the datatype you should create as a struct).
So, write a C++ program (use struct and dynamic memory allocation) that reads N customer records from a text file (customers.txt) such as each record has 4 fields (pieces of information) as shown below:
Account Number (integer)
Customer full name (string)
Customer email (string)
Account Balance (double)
The program is expected to print the records in the format shown below, sorted in decreasing order based on the account balance:
Account Number : 1201077
Name : Jonathan I. Maletic
Email : [email protected]
Balance : 10,000.17
-------------------------------------
Note: The records stored in the following format (4 lines for each account). You need to create a text file that contains sample records as explained in class:
First Line is the account number
Second Line is full name
Third line is email
Forth line is the available balance
Important Note: You must create a struct called (Customer) with enough members (to hold record information) and use it to declare a dynamic array to store all the records (information) read from the file. Then sort the array and then print it. Use the example shared in class (see the slides) as a model for your program.
Example (data saved in text file) |
Output: |
1201077 Jonathan I. Maletic 10,000.17 1991999 John Smith 5,000.11 1333333 Bill Bultman 120,000.00 |
Account Number : 1333333 Name : Bill Bultman Email : [email protected] Balance : 120,000.00 ------------------------------------- Account Number : 1201077 Name : Jonathan I. Maletic Email : [email protected] Balance : 10,000.17 ------------------------------------- Account Number : 1991999 Name : John Smith Email : [email protected] Balance : 5,000.11 ------------------------------------- |
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// customers.txt
1201077
Jonathan I. Maletic
[email protected]
10,000.17
1991999
John Smith
[email protected]
5,000.11
1333333
Bill Bultman
[email protected]
120,000.00
====================================
#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
using namespace std;
struct Customer
{
int accNo;
string name;
string email;
double bal;
};
void sortBasedOnBal(Customer c[],int cnt);
int main() {
//setting the precision to two
decimal places
std::cout << std::setprecision(2) <<
std::fixed;
string name,mail,str,str1;
int id,cnt=0;
double currency;
ifstream dataIn;
dataIn.open("customers.txt");
//checking whether the file name is
valid or not
if(dataIn.fail())
{
cout<<"** File Not Found **";
return 1;
}
else
{
while(getline(dataIn,str))
{
getline(dataIn,name);
getline(dataIn,mail);
getline(dataIn,str);
cnt++;
}
dataIn.close();
// Creating array dynamically
Customer* c = new Customer[cnt];
dataIn.open("customers.txt");
//Reading the data from the file
for(int i=0;i<cnt;i++)
{
getline(dataIn,str);
id=atoi(str.c_str());
c[i].accNo=id;
getline(dataIn,name);
c[i].name=name;
getline(dataIn,mail);
c[i].email=mail;
getline(dataIn,str);
int indx=str.find(",");
if(indx!=-1)
{
str1=str.substr(0,indx);
str1=str1+str.substr(indx+1,str.length()-1);
currency=atof(str1.c_str());
c[i].bal=currency;
}
else
{
currency=atof(str.c_str());
c[i].bal=currency;
}
}
dataIn.close();
sortBasedOnBal(c,cnt);
for(int i=0;i<cnt;i++)
{
cout<<setw(20)<<left<<"Account
Number :"<<c[i].accNo<<endl;
cout<<setw(20)<<left<<"Name
:"<<c[i].name<<endl;
cout<<setw(20)<<left<<"Email
:"<<c[i].email<<endl;
cout<<setw(20)<<left<<"Balance
:"<<c[i].bal<<endl;
cout<<"________________________________________"<<endl;
}
}
return 0;
}
void sortBasedOnBal(Customer c[],int cnt)
{
Customer temp;
//This Logic will Sort the Array of
elements in Descending order
for (int i = 0; i < cnt; i++)
{
for (int j = i + 1; j < cnt; j++)
{
if (c[i].bal < c[j].bal)
{
temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
}
=====================================
Output:
=====================Could you plz rate me well.Thank
You