Question

C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales...

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 require no interaction with the user. Write output to a file named Summary.txt.

3. Sales Records are in a text file named SalesData.txt. You MUST use this exact file name and format for the input. The first entry will be an integer that tells how many sales people there are. Next will be an integer indicating the number of weeks of data for each salesperson. For each salesperson, there will be the sales person's first name and last name, followed by the data for that salesperson. 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

2

firstName1 lastName1

20.00 25.00 30.90 40.00 55.50

20.00 25.00 30.90 40.00 55.50

firstName2 lastname2

30.00 24.00 45.00 67.00 65.50 20.00

25.00 30.90 40.00 55.50

firstName3 lastName3

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 up to 10, 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 named Summary.txt. 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:

o The number of sales people who were processed

o The number of weeks of sales that were processed

o The last name only of each sales person followed by total and average sales for each week

o Grand total sales and average sales per salespersons over all weeks

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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

Add a comment
Know the answer?
Add Answer to:
C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Programming Programming Project Outcomes:  Understand and use C++ functions for procedural abstraction and Functional...

    C++ Programming Programming Project Outcomes:  Understand and use C++ functions for procedural abstraction and Functional Decomposition  Develop some functions for generating numerical data from numerical inputs  Understand and utilize file input and file output to perform computing tasks Requirements: 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...

  • Write a program that stores the weekly (Monday thru Friday) sales totals for three salespersons. Your...

    Write a program that stores the weekly (Monday thru Friday) sales totals for three salespersons. Your program should allow the user to enter the sales amounts and print a sales report with headings, the daily totals for each salesperson (your two-dimensional array), the calculated weekly totals for each sales person and the calculated totals for the day of each salesperson. In addition, create a single-dimensional array of Strings representing the days of the week (Monday-Friday) using an initializer list. Your...

  • In C++. Write another program in file B2.cpp to calculate and print the total sales of...

    In C++. Write another program in file B2.cpp to calculate and print the total sales of each product by each salesperson. Read the sales slips from the file Salesslips.txt (or for partial points, in case your program Bl.cpp does not work correctly, from an array with the same data, initialized in your main program) and calculate the total sales of each product by each salesperson. Accumulate the total sales of each product by each salesperson in a two- dimensional array...

  • 1. Write a C++ program that reads daily weather observation data from a file and writes...

    1. Write a C++ program that reads daily weather observation data from a file and writes monthly and annual summaries of the daily data to a file. a. The daily weather data will be contained in a file named wx_data.txt, with each line of the file representing the weather data for a single day. b. For each day, the date, precipitation amount, maximum temperature, and minimum temperature will be provided as tab-separated data with the following format: 20180101 0.02 37...

  • Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.t...

    Write a program that will first receive as input the name of an input file and an output file. It will then read in a list of names, id #s, and balances from the input file specified (call it InFile.txt) which you will create from the data provided below. The program will then prompt the user for a name to search for, when it finds the name it will output to a file (call it OFile.txt) the person’s id#, name,...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

  • Write a PYTHON program that asks the user for the name of the file. The program...

    Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...

  • In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, t...

    In C++ Write a menu driven C++ program to read a file containing information for a list of Students, process the data, then present a menu to the user, and at the end print a final report shown below. You may(should) use the structures you developed for the previous assignment to make it easier to complete this assignment, but it is not required. Required Menu Operations are: Read Students’ data from a file to update the list (refer to sample...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT