Question

In: Computer Science

Program for C++ A company has six salespeople. Every month, they go on road trips to...

Program for C++

A company has six salespeople. Every month, they go on road trips to sell the company’s product. At the end of each month, the total sales for a salesperson in that month, together with that salesperson’s ID and the month, is recorded in a file. At the end of each year, the manager of the company wants to see an annual sales report. Due to the amount of data involved, he’s like to be able to view the report by ID or by total sales.   

Create a program to load id data from salesID.txt, sales data from salesData.txt (unknown # of records. The records are not in any order of sales id or month), calculate the total sales of each person in a year, and display a report. Shell code is provided in HW8_SalesReport_shell.cpp. You need understand the shell code and then add:

  • ADD CODE #1: declaration of two functions sortByID and showReport. Also include:
    • Function prologue comment
    • /*IN*/, /*OUT*/, /*INOUT*/ comments to each function parameter.
    • Pre- and Post- condition comments for each function.
  • ADD CODE #2: complete function loadData
  • ADD CODE #3: implementation of two functions sortByID and showReport

You’re not supposed to modify the given code otherwise

salesID.txt
32214
23422
57373
35864
54654
12345

salesData.txt
12345 1 893
32214 1 343
23422 3 903
57373 2 893
35864 5 329
54654 9 392
12345 2 999
32214 4 892
23422 4 895
23422 2 492
57373 6 892
35864 10 1223
54654 11 3420
12345 12 322
35864 5 892
54654 3 893
12345 8 494
32214 8 9023
23422 6 223
23422 4 783
57373 8 8834
35864 3 2882

HW8_SalesReport_shell.cpp

// HW8_SalesReport_shell.cpp (shell code. need ADD CODE #1 ~ #3)
// by Bin "Crystal" Peng, CS225
// generate sales report using monthly sales data
// fixed # of sales people; unknown # of sales record


#include <iostream>
#include <fstream> // std::ifstream
#include <string> // std::string
#include <iomanip> // std::setw(), std::fixed, std::setprecision()

//----------------------------------------------
// global declarations
//----------------------------------------------

const int MAX = 10; // max # of sales person

struct salesPersonRec
{
std::string ID; // salesperson's ID
double totalSales; // salesperson's yearly sales amount
};


//----------------------------------------------
// function declarations
//----------------------------------------------

// read id data into array and set totalSales of each record to 0
bool loadID(salesPersonRec list[]/*OUT*/, int listSize/*IN*/);
// Pre: listSize specified
// Post: listSize # of ids read in from file and saved in the ID column in the first listSize records of list. totalSales of those records set to 0

// read in sales data and calculate total sales per person
bool loadData(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/);
// Pre: listSize specified, list contains valid ids and totalSales set to 0 for the first listSize records
// Post: sales data read from file
// totalSales of the first listSize records are set to accumulated sales of the salesperson with the given id

// display menu on screen
void showMenu();
// Pre: none
// Post: menu choices printed on screen

// display id and yearly sales amout of all salespersons by id order
void showReportByID(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/);
// Pre: listSize specified, list contains at least listSize number of records
// Post: the first listSize records in list are reordered so they are now in ascending order of the id column
// id and totalSales column of the first listSize records in list are printed on screen

// display id and yearly sales amout of all salespersons by total sales order
void showReportBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/);
// Pre: listSize specified, list contains at least listSize number of records
// Post: the first listSize records in list are reordered so they are now in ascending order of the total sales column
// id and totalSales column of the first listSize records in list are printed on screen

// sort sales person records by total sales amount
void sortBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/);
// Pre: listSize specified, list contains at least listSize number of records
// Post: the first listSize records in list are reordered so they are now in ascending order of the totalSales column


// ADD CODE #1: two function declarations

// END ADD CODE #1

//----------------------------------------------

int main()
{
salesPersonRec salesPersonList[MAX]; // array to hold the salesperson's data
int numOfSalesPerson = 6;

if (!loadID(salesPersonList, numOfSalesPerson)) // load id data
return 1;

if (!loadData(salesPersonList, numOfSalesPerson)) // load sales data
return 1;

// user interaction via menu
int option = 0; // user option
do
{
// display menu
showMenu();

// user option
std::cin >> option;

switch (option)
{
case 1: // sort and display report by id
showReportByID(salesPersonList, numOfSalesPerson);
break;
case 2: // sort and display report by sales
showReportBySales(salesPersonList, numOfSalesPerson);
break;
case 0: // exit
std::cout << "\nThank you for using our reporting system!\n";
break;
default: // invalid input
std::cout << "invalid choice. Please try again.\n";
break;
}
} while (option != 0);

} // end main


//----------------------------------------------
// Function Implementation
//----------------------------------------------

// read id data into array and set totalSales of each record to 0
bool loadID(salesPersonRec list[]/*OUT*/, int listSize/*IN*/)
{
std::ifstream inFile; // input file
std::string idfilename = "salesID.txt";

// open id file
inFile.open(idfilename);
if (inFile.fail())
{
std::cerr << "Error: can't open input file \"" << idfilename << "\". Abort. \n"; // report to cerr
return false; // and end now
}

// read in id data
for (int index = 0; index < listSize; index++)
{
if (!(inFile >> list[index].ID)) // get salesperson's ID
return false; // reading failed

list[index].totalSales = 0.0;
}
inFile.close(); // close file

return true;
} //end loadID

//----------------------------------------------

// read in sales data and calculate total sales of each person
bool loadData(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/)
{
std::ifstream inFile; // input file
std::string datafilename = "salesData.txt";

// open sales data
inFile.open(datafilename);
if (inFile.fail())
{
std::cerr << "Error: can't open input file \"" << datafilename << "\". Abort. \n"; // report to cerr
return false; // and end now
}

// read in data and process
std::string id;
int month;
double amount;

// ADD CODE #2:
// read sales data from file,
// calculate and update totalSales of each record


// END ADD CODE #2


inFile.close(); // close data file

return true;
} //end loadData

//----------------------------------------------

// display menu on screen
void showMenu()
{
const std::string menuOptions[] = { "Options:",
"1. Display sales report by sales id.",
"2. Display sales report by annual sales amount.",
"0. Exit",
"Choose (0 ~ 2): "
};
int menuLen = sizeof(menuOptions) / sizeof(std::string);

std::cout << std::endl;
for (int i = 0; i < menuLen - 1; i++)
std::cout << menuOptions[i] << std::endl;
std::cout << menuOptions[menuLen - 1]; // last item
} // end getUserOption

//----------------------------------------------

void showReportByID(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/)
{
sortByID(list, listSize);
showReport(list, listSize);
} // end showReportByID

//----------------------------------------------

void showReportBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/)
{
sortBySales(list, listSize);
showReport(list, listSize);
} // end showReportBySales

//----------------------------------------------

void sortBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/)
{
// insertion sort
for (int i = 1; i < listSize; i++)
{
// find spot to insert [i]
salesPersonRec hold = list[i];
double amount = list[i].totalSales;
int indexToInsert = i - 1;
while (indexToInsert >= 0 && amount < list[indexToInsert].totalSales)
{
// shift
list[indexToInsert + 1] = list[indexToInsert];
indexToInsert--;
}
// insert
list[indexToInsert + 1] = hold;
}
} // end sortBySales

//----------------------------------------------

// ADD CODE #3: implementation of two functions

// END ADD CODE #3

Solutions

Expert Solution

HW8_SalesReport_shell.cpp

// Add code is bold text

//===================================================

#include <iostream>
#include <fstream> // std::ifstream
#include <string> // std::string
#include <iomanip> // std::setw(), std::fixed, std::setprecision()

//----------------------------------------------
// global declarations
//----------------------------------------------

const int MAX = 10; // max # of sales person

struct salesPersonRec
{
   std::string ID; // salesperson's ID
   double totalSales; // salesperson's yearly sales amount
};


//----------------------------------------------
// function declarations
//----------------------------------------------

// read id data into array and set totalSales of each record to 0
bool loadID(salesPersonRec list[]/*OUT*/, int listSize/*IN*/);
// Pre: listSize specified
// Post: listSize # of ids read in from file and saved in the ID column in the first listSize records of list. totalSales of those records set to 0

// read in sales data and calculate total sales per person
bool loadData(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/);
// Pre: listSize specified, list contains valid ids and totalSales set to 0 for the first listSize records
// Post: sales data read from file
// totalSales of the first listSize records are set to accumulated sales of the salesperson with the given id

// display menu on screen
void showMenu();
// Pre: none
// Post: menu choices printed on screen

// display id and yearly sales amout of all salespersons by id order
void showReportByID(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/);
// Pre: listSize specified, list contains at least listSize number of records
// Post: the first listSize records in list are reordered so they are now in ascending order of the id column
// id and totalSales column of the first listSize records in list are printed on screen

// display id and yearly sales amout of all salespersons by total sales order
void showReportBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/);
// Pre: listSize specified, list contains at least listSize number of records
// Post: the first listSize records in list are reordered so they are now in ascending order of the total sales column
// id and totalSales column of the first listSize records in list are printed on screen

// sort sales person records by total sales amount
void sortBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/);
// Pre: listSize specified, list contains at least listSize number of records
// Post: the first listSize records in list are reordered so they are now in ascending order of the totalSales column

// ADD CODE #1: two function declarations

// sort sales person records by ID of salesperson
void sortByID(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/);
// Pre: listSize specified, list contains at least listSize number of records
// Post: the first listSize records in list are reordered so they are now in ascending order of the ID column

// display id and yearly sales amout of all salespersons
void showReport(salesPersonRec list[]/*IN*/, int listSize/*IN*/);
// Pre: listSize specified, list contains at least listSize number of records
// Post: first listSize records in list are printed on screen with salesperson id and totalSales column

// END ADD CODE #1

//----------------------------------------------

int main()
{
   salesPersonRec salesPersonList[MAX]; // array to hold the salesperson's data
   int numOfSalesPerson = 6;

   if (!loadID(salesPersonList, numOfSalesPerson)) // load id data
       return 1;

   if (!loadData(salesPersonList, numOfSalesPerson)) // load sales data
       return 1;

   // user interaction via menu
   int option = 0; // user option
   do
   {
       // display menu
       showMenu();

       // user option
       std::cin >> option;

       switch (option)
       {
       case 1: // sort and display report by id
           showReportByID(salesPersonList, numOfSalesPerson);
           break;
       case 2: // sort and display report by sales
           showReportBySales(salesPersonList, numOfSalesPerson);
           break;
       case 0: // exit
           std::cout << "\nThank you for using our reporting system!\n";
           break;
       default: // invalid input
           std::cout << "invalid choice. Please try again.\n";
           break;
       }
   } while (option != 0);

} // end main


//----------------------------------------------
// Function Implementation
//----------------------------------------------

// read id data into array and set totalSales of each record to 0
bool loadID(salesPersonRec list[]/*OUT*/, int listSize/*IN*/)
{
   std::ifstream inFile; // input file
   std::string idfilename = "salesID.txt";

   // open id file
   inFile.open(idfilename);
   if (inFile.fail())
   {
       std::cerr << "Error: can't open input file \"" << idfilename << "\". Abort. \n"; // report to cerr
       return false; // and end now
   }

   // read in id data
   for (int index = 0; index < listSize; index++)
   {
       if (!(inFile >> list[index].ID)) // get salesperson's ID
           return false; // reading failed

       list[index].totalSales = 0.0;
   }
   inFile.close(); // close file

   return true;
} //end loadID

//----------------------------------------------

// read in sales data and calculate total sales of each person
bool loadData(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/)
{
   std::ifstream inFile; // input file
   std::string datafilename = "salesData.txt";

   // open sales data
   inFile.open(datafilename);
   if (inFile.fail())
   {
       std::cerr << "Error: can't open input file \"" << datafilename << "\". Abort. \n"; // report to cerr
       return false; // and end now
   }

   // read in data and process
   std::string id;
   int month;
   double amount;

   // ADD CODE #2:
   // read sales data from file,
   // calculate and update totalSales of each record
   while (!inFile.eof())   // checking for end of file
   {
       if (!(inFile >> id)) // get salesperson's ID
           return false; // reading failed

       if (!(inFile >> month)) // get salesperson's month
           return false; // reading failed

       if (!(inFile >> amount)) // get salesperson's sales amount for that month
           return false; // reading failed

       for (int index = 0; index < listSize; index++) // for loop for searching id in list
       {
           if (list[index].ID == id)
           {
               list[index].totalSales += amount; // adding amount for that salesperson
               break;
           }
       }          
   }

   // END ADD CODE #2


   inFile.close(); // close data file

   return true;
} //end loadData

//----------------------------------------------

// display menu on screen
void showMenu()
{
   const std::string menuOptions[] = { "Options:",
       "1. Display sales report by sales id.",
       "2. Display sales report by annual sales amount.",
       "0. Exit",
       "Choose (0 ~ 2): "
   };
   int menuLen = sizeof(menuOptions) / sizeof(std::string);

   std::cout << std::endl;
   for (int i = 0; i < menuLen - 1; i++)
       std::cout << menuOptions[i] << std::endl;
   std::cout << menuOptions[menuLen - 1]; // last item
} // end getUserOption

//----------------------------------------------

void showReportByID(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/)
{
   sortByID(list, listSize);
   showReport(list, listSize);
} // end showReportByID

//----------------------------------------------

void showReportBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/)
{
   sortBySales(list, listSize);
   showReport(list, listSize);
} // end showReportBySales

//----------------------------------------------

void sortBySales(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/)
{
   // insertion sort
   for (int i = 1; i < listSize; i++)
   {
       // find spot to insert [i]
       salesPersonRec hold = list[i];
       double amount = list[i].totalSales;
       int indexToInsert = i - 1;
       while (indexToInsert >= 0 && amount < list[indexToInsert].totalSales)
       {
           // shift
           list[indexToInsert + 1] = list[indexToInsert];
           indexToInsert--;
       }
       // insert
       list[indexToInsert + 1] = hold;
   }
} // end sortBySales

//----------------------------------------------

// ADD CODE #3: implementation of two functions

void sortByID(salesPersonRec list[]/*INOUT*/, int listSize/*IN*/)
{
   // insertion sort
   for (int i = 1; i < listSize; i++)
   {
       // find spot to insert [i]
       salesPersonRec hold = list[i];
       std::string id = list[i].ID;
       int indexToInsert = i - 1;
       while (indexToInsert >= 0 && (strcmp(id.c_str(), (list[indexToInsert].ID).c_str())<0))
       { // strcmp(s1,s2) compare char* return 0 if equal , negative if s1<s2 and positive if s1>s2

           //c_str() convert string to char*
           // shift
           list[indexToInsert + 1] = list[indexToInsert];
           indexToInsert--;
       }
       // insert
       list[indexToInsert + 1] = hold;
   }
} // end of sortByID

//----------------------------------------------

void showReport(salesPersonRec list[]/*IN*/, int listSize/*IN*/)
{
   std::cout << "Salesperson Id\t | \tTotal Sales" << std::endl;
   for (int index = 0; index < listSize; index++) // index for iterating over first listSize ids
   {
       std::cout << "    " <<list[index].ID << "\t | \t " << list[index].totalSales << std::endl;

        // extra spaces for alignment in table
   }
} //end of showReport

//----------------------------------------------

// END ADD CODE #3

//===================================================

Sample input files 1:

salesID.txt
32214
23422
57373
35864
54654
12345

salesData.txt
12345 1 893
32214 1 343
23422 3 903
57373 2 893
35864 5 329
54654 9 392
12345 2 999
32214 4 892
23422 4 895
23422 2 492
57373 6 892
35864 10 1223
54654 11 3420
12345 12 322
35864 5 892
54654 3 893
12345 8 494
32214 8 9023
23422 6 223
23422 4 783
57373 8 8834
35864 3 2882

Sample output 1:

Sample input files 2:

salesID.txt
A123
B245
A745
B874
7451
7964

salesData.txt
A123 1 400
B245 1 200
A745 1 100
B874 1 240
7451 1 300
7964 1 150
A123 2 700
B245 3 500
A745 4 100
B874 5 240
7451 6 100
7964 7 250

Sample output for input 2:


Related Solutions

C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople...
C++ Program (Using 2D array and bubble sort to sort data) A company pays its salespeople on a commission basis.  The salespeople each receive $250 per week plus 11 percent of their gross sales for the sales period.  For example, a salesperson who grosses $5000 in sales in the period receives $250 plus 11 percent of $5000, or a total of $812.21.  Write a program (using an array of counters) determines for each salesperson their total sales, their salary and additional data points.  There...
If Paige has accumulated $6200 by saving $80 every month for six ​years, what nominal annual...
If Paige has accumulated $6200 by saving $80 every month for six ​years, what nominal annual rate of interest compounded semi-annually has been​ earned? The nominal annual rate of interest is _____%compounded semi-annually
Assume that a bond will make payments every six months as shown on the following timeline (using six-month periods)
Assume that a bond will make payments every six months as shown on the following timeline (using six-month periods):The timeline starts at Period 0 and ends at Period 50. The timeline shows a cash flow of $ 20.92 each from Period 1 to Period 49. In Period 50, the cash flow is $ 20.92 plus $ 1,000.Period0124950Cash Flows$20.92$20.92$20.92$20.92+$1,000a. What is the maturity of the bond (in years)?b. What is the coupon rate (as a percentage)?c. What is the face value?
The current price of the stock of Bufflehead company is C$100. During each six-month period it...
The current price of the stock of Bufflehead company is C$100. During each six-month period it will either rise by 10% or fall by 10%. The interest rate is 6% per annum compounded semi-annually. a. Calculate the value of a one-year European put option on Bufflehead's stock with an exercise price of C$115. b. Recalculate the value of the Bufflehead put option, assuming that it is an American option.
Suppose a company has the following sales for the last six months (January – June): Month...
Suppose a company has the following sales for the last six months (January – June): Month Period Sales January 1 16 February 2 25 March 3 20 April 4 20 May 5 31 June 6 35 July 7 26 August 8 (a) Use exponential smoothing (a= 0.10) to forecast the sales for August (to 2 decimals). What is the value? Compute the MSE for the exponential smoothing method from the above question (to 2 decimals). Use a 2-period moving average...
write a program on c++ that outputs a calendar for a given month in a given...
write a program on c++ that outputs a calendar for a given month in a given year, given the day of the week on which the 1st of the month was. The information in numeric format (months are: 1=Januay, 2=February 3=March, 4= April, 5= May, 6= June, 7= July... etc days are 1=Sunday, 2=Monday, 3= Tuesday, 4= Wednesday, 5= Thursday, 6= Friday and 7= Saturday ). The program output that month’s calendar, followed by a sentence indicating on which day...
First The sales department of a cellular phone company pays its salespeople $1,500 per month plus...
First The sales department of a cellular phone company pays its salespeople $1,500 per month plus 25 percent of each new subscriber’s first month’s billings. A new subscriber’s first-month bill averages $80. Salespeople work 160 hours a month (four weeks at 40 hours per week). If salespeople work more than 160 hours per month, they receive $12 per hour for hours in excess of 160. Sales leads for the sales department are generated in a variety of ways—direct mailings to...
use C++ Write a program to calculate the frequency of every letter in an input string...
use C++ Write a program to calculate the frequency of every letter in an input string s. Your program should be able to input a string. Calculate the frequency of each element in the string and store the results Your program should be able to print each letter and its respective frequency. Identify the letter with the highest frequency in your message. Your message should be constructed from at least 50 letters. Example of Output: letters frequency a 10 b...
Every C++ program starts with this function:____________________ 2. If A is > 100 I want cnt...
Every C++ program starts with this function:____________________ 2. If A is > 100 I want cnt to be equal to 32 but if A 100 or less I want cnt to be 2. Write a single line using a conditional operator to do this. 3. I want to store into B the remainder of dividing B by the total of A plus D. Write the statement: as a combined operator statement not using a combined operator 4. I want to...
Write a C program that loops and asks you every time to enter the name of...
Write a C program that loops and asks you every time to enter the name of a text file name, then it will read the file and perform statistics on its contents and display the results to the screen and to an output file called out.txt. The input files can be any text file. The program should calculate these integers as it reads character by character as we did in the last class example filecopy and make use of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT