Write a program that processes the data of a retail company with following requi
ID: 3762248 • Letter: W
Question
Write a program that processes the data of a retail company with following requirements: Define a new data type (struct) of Customer, with following members: ID, first name, last name, number of items purchased, price of a single item, and total spending. In the main function, declare an array of Customer. The array should be created dynamically. In other words, you do not know how many customers when you write the program. The program can asks a user to enter a number, or better yet, let the code to count the number of records in the file. The main function reads the data from a file of "customers.dat". Each row represents a customers record, with ID, first name, last name, number of item purchased and the price of a single item. The data columns are separated by white spaces. Store the customer data in the array. Design a function, computeTotals, to calculate the total spending of each customer: computeTotals takes two parameters, the array and the number of customers. computeTotals must be implemented with pointer notation (3-point deduction for using nonpointer notations). In the main function, print out all records, including the total spending, such as:Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//Custoer struct declared
struct Customer
{
int id;
std::string firstName;
std::string lastName;
float noItems;
float price;
float totalSpend;
};
//computing totals....
void computeTotals(Customer *arr[], int total){
for(int i=0;i<total;i++){
arr[i].totalSpend = arr[i].noItems * arr[i].price;
}
}
int main()
{
Customer * pointer = new Customer[100];
//reading file
ifstream inFile;
inFile.open("customers.dat");
if (!inFile) { //if cannot open
cout << "Unable to open file";
exit(1); //exiting from app
}
string firstName,lastName;
float noItems,price;
int count=0,id;
//reading data from file
while (inFile >> id >> last_name >> first_name >> noItems >> price) {
pointer[count].id = id;
pointer[count].firstName = firstName;
pointer[count].lastName = lastName;
pointer[count].noItems = noItems;
pointer[count].price = price;
pointer[count].totalSpend = 0;
count++;
}
//calling function to calcualte total spends
computeTotals(&pointer,count);
//printing details
for(int i=0;i<count;i++){
cout<<" "<<pointer[i].id<<" "<<pointer[i].firstName<<" "<<pointer[i].lastName<<" "<<pointer[i].noItems<<" "<<pointer[i].price<<" "<<pointer[i].totalSpend;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.