Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Objective: Write a program to read a pre-specified file containing salespers

ID: 3854443 • Letter: C

Question

C++ Objective:

Write a program to read a pre-specified file containing salespersons' names and daily sales for some number of weeks. It will create an output file with a file name you have gotten from the user that will hold a summary of sales data.

Specifications:

1.  You should do a functional decomposition to identify the functions that you will use in the program. These should include reading and writing the files, tallying and showing statistics, etc.

2.  The program should prompt the user for the output file name only. Obviously, this should be done in a function.

3.   Sales Records are in a text file named theSales.txt. You MUST use this exact file name for the input. The first entry will be an integer that tells how many sales people there are. Following that will be a sales person's last name and first name, followed by the number of weeks of data for that salesperson. Next will be daily sales for the proper number of weeks. Each week will have FIVE days of sales data as doubles. So, a file for 3 sales people and 2 weeks might look like this:

3

lastName1 firstName1

2

20.00 25.00 30.90 40.00 55.50

20.00 25.00 30.90 40.00 55.50

lastname2 firstName2

3

30.00 24.00 45.00 67.00 65.50

20.00 25.00 30.90 40.00 55.50

56.90 87.00 43.50 56.98 55.40

lastName3 firstName3

2

62.00 34.50 12.50 34.00 34.90

70.00 80.00 90.00 65.00 39.00

4.  The program must be able to handle however many salespeople are specified (1 to 10 salespeople) and however many weeks of data are specified (1 to 10 weeks). The data must be organized exactly as specified above. Be sure to read the file that way. I will test your program with a file having that format.

5.    The output will go to an output file. You will get the name of the file from the user. You will invent the format for the output file. The file must contain the following, all of which must be properly labeled so that it is easy to know what the numbers refer to:

- The number of sales people who were processed

- The number of weeks of sales that were processed

- The last name only of each sales person followed by total and average sales for each week
- Grand total sales and average sales per week for all salespersons over all weeks

6.      If the input file or output file opening fails, print an error message and exit.

Notes:

1. You will need to declare several variables. Try to use the least number of variables possible. For instance, you WILL NOT need separate variables for each salesperson's average and total sales. Simply reuse the same variables for all.

2. You may assume that all weeks will have 5 days worth of sales reported.

3. You can assume the correct number of names and middle initials will be there and the correct number of sales figures.

4. Note that you should test your program on data that is easy to hand tally so that you know the correct output you SHOULD get before your program ever runs. That is the ONLY way to know that your program works properly. The above data is NOT good data to use.

Explanation / Answer

Given below is the code for the question. In case of any issues, please post a comment, I will respond. If happy with the answer, please rate it. Thank you.

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <iomanip>

using namespace std;

typedef struct

{

string firstname;

string lastname;

int noWeeks;

double salesdata[10][5] ; //max 10 weeks with each having 5 days

}salesman;

//loads sales man data from specified file and retures the number of salesmen loaded

int loadfile(string filename, salesman salesmen[]);

void process(salesman salesmen[], int count);

int main()

{

string infilename = "/Users/raji/Documents/Chegg/c++/Test/Test/theSales.txt";

ifstream infile;

int count;

salesman salesmen[10]; //max 10 salesmen

  

count = loadfile(infilename, salesmen);

process(salesmen, count);

  

}

int loadfile(string filename, salesman salesmen[])

{

fstream infile;

int count;

  

infile.open(filename.c_str());

if(!infile.is_open())

{

cout << "ERROR: could not open file " << filename << endl;

exit(1);

}

  

infile >> count; // read the number of salesmen

  

for(int idx = 0; idx < count; idx++)

{

infile >> salesmen[idx].lastname >> salesmen[idx].firstname >> salesmen[idx].noWeeks;

for(int week = 0; week < salesmen[idx].noWeeks; week++) // for each week

{

for(int day = 0; day < 5; day++) // for each day in the week

infile >> salesmen[idx].salesdata[week][day];

}

}

  

infile.close();

  

return count;

}

void process(salesman salesmen[], int count)

{

double smTotal, smAvg ; //for an individual salesman

int totalWeeks = 0;

double grandTotalSales = 0, grandAvgSales = 0; //for overall (all the weeks of all salesmen

string filename;

ofstream outfile;

  

cout << "Enter output filename: " ;

cin >> filename;

  

outfile.open(filename.c_str());

if(!outfile.is_open())

{

cout << "Could not open output file " << filename << endl;

exit(1);

}

  

outfile << "Number of Salemen: " << count << endl;

  

outfile << fixed << setprecision(2);

  

for(int i = 0; i < count; i++)

{

outfile << salesmen[i].lastname << endl;

  

for(int week = 0; week < salesmen[i].noWeeks; week++)

{

//calculate weekly total and avg for the current saleman

smTotal = 0;

for(int day = 0; day < 5; day++)

{

smTotal += salesmen[i].salesdata[week][day];

  

}

grandTotalSales += smTotal;

  

smAvg = smTotal / 5;

  

outfile << smTotal << " " << smAvg << endl;

  

}

totalWeeks += salesmen[i].noWeeks;

}

grandAvgSales = grandTotalSales / totalWeeks;

outfile << "The total number of weeks altogether: " << totalWeeks << endl;

outfile << "The grand total sales: " << grandTotalSales << endl;

outfile << "Averge sales per week: " << grandAvgSales << endl;

  

outfile.close();

cout << "Finished processing. Check output file " << filename << endl;

}

input file : theSales.txt

3
lastName1 firstName1
2
20.00 25.00 30.90 40.00 55.50
20.00 25.00 30.90 40.00 55.50
lastname2 firstName2
3
30.00 24.00 45.00 67.00 65.50
20.00 25.00 30.90 40.00 55.50
56.90 87.00 43.50 56.98 55.40
lastName3 firstName3
2
62.00 34.50 12.50 34.00 34.90
70.00 80.00 90.00 65.00 39.00

output

$./a.out

Enter output filename: salesout.txt
Finished processing. Check output file salesout.txt

$ cat salesout.txt
Number of Salemen: 3
lastName1
171.40 34.28
171.40 34.28
lastname2
231.50 46.30
171.40 34.28
299.78 59.96
lastName3
177.90 35.58
344.00 68.80
The total number of weeks altogether: 7
The grand total sales: 1567.38
Averge sales per week: 223.91

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote