Write a program with c++ t hat processes the data of a retail company.Make sure
ID: 3736136 • Letter: W
Question
Write a program with c++ that processes the data of a retail company.Make sure your code satisfies all the requirements below.
(Also Please explain me how and where your read read the data from,do you first save the data and where and with what prefix and
how you read from it? then where you save the data(the result), with what prefix and whre?)
program:
1. Define a new data type of Customer using struct, with the following members: ID, first name,
last name, number of items purchased, price of a single item, and total spending.
2. In the main function, declare an array of Customer.
3. The array should be created dynamically. In other words, you do not know how many customers when you write the program. The program can ask a user to enter the total number of customers, or better yet, let the code to count the number of records in the file.
4. The main function reads the data from a file: “retails.txt”.
Each row represents a customer’s record, with ID, first name, last name, number of item purchased and the price of a single item.
The data columns are separated by whitespaces.
5. Store the customer data in the array.
6. Design a function, computeTotals, to calculate the total spending of each customer:
total = numItems * price.
7. computeTotals takes two parameters: an array of Customer and the number of customers.
8. computeTotals must be coded with pointer notation (a 3-point deduction for non-pointer notations).
9. In the main function, print out all records, including the total spending, such as:
1001 Scott Adams 3 22.86 68.58
1002 Nicholas Allen 8 10.00 80.00
TXT doc
Explanation / Answer
Please find the code below with detailed inline comments.
CODE
=====================
#include <iostream>
#include <fstream>
using namespace std;
// create a structure for Customer
struct Customer {
int id;
string fname, lname;
int numberItems;
float price, total_spending;
};
using namespace std;
void computeTotals(struct Customer* customers, int size) {
for(int i=0; i<size; i++) {
int n = customers[i].numberItems;
float p = customers[i].price;
customers[i].total_spending = p * n;
}
}
int main() {
struct Customer* customers; //array of customers
int records = 0; // number of records in the file
ifstream inFile; // to open the file in read mode
inFile.open("Customers.txt"); // Open Customers.txt input file
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
string line;
//find the number of records
while (getline(inFile, line))
++records;
customers = new struct Customer[records];
int ID, items;
string fn, ln;
float p;
int i = 0;
// store the input data into Customer array
while(inFile >> ID >> fn >> ln >> items >> p) {
struct Customer c;
c.id = ID;
c.fname = fn;
c.lname = ln;
c.numberItems = items;
c.price = p;
customers[i++] = c;
}
computeTotals(customers, records);
for(i=0; i<records; i++) {
cout << customers[i].id << " " << customers[i].fname << " "
<< customers[i].lname << " " << customers[i].numberItems << " "
<< customers[i].price << " " << customers[i].total_spending << endl;
}
// close file
inFile.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.