Question

In: Computer Science

In this assignment, you are expected to rewrite the program from assignment7 using structs. That is,...

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

[email protected]

10,000.17

1991999

John Smith

[email protected]

5,000.11

1333333

Bill Bultman

[email protected]

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

-------------------------------------

  • Make sure your programs adhere to proper programming style (e.g., good identifiers, comments, etc.)

Solutions

Expert Solution

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


Related Solutions

The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic...
The League with DMA Rewrite your League program from Assignment 8 so that it uses Dynamic Memory Allocation (DMA) to create the team names and scores arrays. This is a good test of the modularity of your program. You will only need to make slight modifications to your main() function if you wrote your original program using functions similar to the following: void initializeData(string names[], int wins[], int size) void sort(string names[], int wins[], int size) void display(string names[], int...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the...
Python - Rewriting a Program Rewrite Program 1 using functions. The required functions are in the table below. Create a Python program that will calculate the user’s net pay based on the tax bracket he/she is in. Your program will prompt the user for their first name, last name, their monthly gross pay, and the number of dependents. The number of dependents will determine which tax bracket the user ends up in. The tax bracket is as follows: 0 –...
Rewrite the grade program from Question 2.2 using a user-defined function called computegrade that takes a...
Rewrite the grade program from Question 2.2 using a user-defined function called computegrade that takes a score as its parameter and returns a grade as a string. (15 points) USING PYTHON LANGUAGE >= 0.9 A >= 0.8 B >= 0.7 C >= 0.6 D < 0.6 F Run the program repeatedly as shown below to test the various different values for input. Enter score: 0.95 A Enter score: perfect Bad score Enter score: 10.0 Bad score Enter score: 0.75 C...
You are expected to write a program from scratch. In the program, an array will be...
You are expected to write a program from scratch. In the program, an array will be initialized with 23 random integers between 1000 and 1999 (inclusive). The output of the program is 4 lines on the screen, specifically, All elements in the array (line 1) All elements in reverse order (line 2) Every element that is less than 1500 and also at an odd index (line 3) Every odd element that is larger than 1500 (line 4) Note that you...
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that...
Having trouble with this assignment: Rewrite your most recent high scores program (shown below) so that each name/score pair is stored in a struct named highscore. Except as noted below, this new program will continue to meet all of the requirements of your most recent high scores program. Your new program should meet the following requirements: The highscore struct should have two fields: an int named score and a char array named name. The char array should have 24 elements,...
For this assignment, you will create flowchart using Flowgorithm to represent the logic of a program...
For this assignment, you will create flowchart using Flowgorithm to represent the logic of a program that allows the user to enter a number of dollars and convert it to Euros and Japanese yen. You will have to do some research on current rates of monetary exchange for this one. Don't forget to declare your variables and use output statements to prompt the user to enter specific values prior to including an input statement. You will use an assignment statement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT