Question

C++ Background information: A company has four salespeople who sell five different products. Once a day,...

C++

Background information:

A company has four salespeople who sell five different products. Once a day, each salesperson passes in a slip for each different type of product sold. First on the line is the salesperson name, second is the product number, and third is the sales volume for this product by this salesperson in dollars. For example, the line Tara 2 200.00 means that salesperson Tara reported sales of volume $200.00 for product number 2.

Data:

Eric 1 200000.00
Sookie 2 200.00
Sookie 4 200.50
Bill 3 5000.00
Bill 5 7500.00
Tara 4 350.50
Eric 2 200.00
Tara 2 200.00
Tara 4 350.50
Bill 5 2500.00
Sookie 1 50000.00
Sookie 2 200.00
Eric 5 10000.00
Tara 2 200.00
Tara 4 150.50
Bill 5 1000.00
Sookie 4 400.50

B1. Make a class SalesSlip to represent the structure of a sales slip. Among the other methods that you need, include a method to read a sales slip from the next line of a text file. Your method should have an argument of type ifstream& representing the text file from which the method reads. Make a main function in the program to test your class.

B2. Write another program to calculate and print the total sales of each product by each salesperson. Read the sales slips from the data (or for partial points, in case your first program 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 with a row for each salesperson and a column for each product. You may use the salesperson’s ID number from the following table as a row number for the corresponding salesperson. Your program should print the 2-dimensional array in a well-formatted table, showing the salesperson’s name in front of each row and the product number above each column.

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

Hello dear...

Here is the perfectly working C++ program for both the tasks. I have also written comments for better understanding. Also attaches images output and code for comfort of reading. Hope you will like it.

SalesSlip.cpp

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
//class definition
class SalesSlip{
        //private data members
        int product;
        string name;
        double volume;
        //public methods
        public:
                //default construtor
                SalesSlip(){
                        name = "";
                        product = 0;
                        volume = 0;
                }
                //function to read data from ifstream
                void read(ifstream& in){
                        in >> name >> product >> volume ;
                }
                //getter methods to access private data values
                string getName(){
                        return name;
                }
                int getProductNumber(){
                        return product;
                }
                double getVolume(){
                        return volume;
                }
                //function to print data
                void print(){
                        cout << name << " " << product << " " << volume << endl;
                }
}; //end of class definition

//main methods
int main(){
        //declare required variaebles
        int i,j;
        //open file into ifstream object
        ifstream in ("sales data.txt");
        //print error message and exit if unable to open the input file
        if (!in.is_open()){
                cout << "Unable to open input file";
                return 0;
        }
        //create a vector(dynamic array) to store array of SalesSlip objects
        vector<SalesSlip> data;
        //run the loop until end of file reaches
    while (!in.eof()){ 
        //create a SalesSlip object and read the input data from ifstream
        SalesSlip slip;
        slip.read(in);
        //add object to vector
        data.push_back(slip); 
    }
        //resizing vector to remove last unneccessary elements
        data.resize(data.size()-1);
        //print data of objects in vector
        for(i = 0; i < data.size(); i++){
                data[i].print();
        }
        
        //storing the salespersons names in an array
        string names[] = {"Eric", "Sookie", "Bill", "Tara"};
        //create table to store total sales of each product by each salesperson
        int table[4][5] = {0};
        
        //calculate total sales  of each product by each salesperson
    for (i = 0; i < data.size(); i++) {
    
        if(data[i].getName() == names[0])
                table[0][data[i].getProductNumber()-1] ++;
        else if(data[i].getName() == names[1])
                table[1][data[i].getProductNumber()-1] ++;
                else if(data[i].getName() == names[2])
                table[2][data[i].getProductNumber()-1] ++;
        else if(data[i].getName() == names[3])
                table[3][data[i].getProductNumber()-1] ++;
    }
    //print table
    cout << "\nTotal sales  of each product by each salesperson" << endl;
        cout << "\t " << 1 << "\t" << 2 << "\t" << 3 << "\t" << 4 << "\t" << 5 ;
        for(i=0; i<4; i++){
                 cout << "\n" << names[i] << "\t|";
                for(j=0; j<5; j++){
                        cout << table[i][j] << "\t";
                }
        }
        
        return 0; //end
}

sales data.txt

Eric 1 200000.00
Sookie 2 200.00
Sookie 4 200.50
Bill 3 5000.00
Bill 5 7500.00
Tara 4 350.50
Eric 2 200.00
Tara 2 200.00
Tara 4 350.50
Bill 5 2500.00
Sookie 1 50000.00
Sookie 2 200.00
Eric 5 10000.00
Tara 2 200.00
Tara 4 150.50
Bill 5 1000.00
Sookie 4 400.50

Output:

Eric 1 200000 Sookie 2 200 Sookie 4 200.5 Bill 3 5000 Bill 5 7500 Tara 4 350.5 Eric 2 200 Tara 2 200 Tara 4 350.5 Bill 5 2500

Images of code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 #include51 52 53 54 55 56 57 58 //create a vector(dynamic array) to store array of SalesSlip objects vector<SalesSlip> data; //run th

Hope you like it.

If you have any doubts ask me in comment section.
If you like my work, please give me a like and feedback. That helps me a lot. Thank you.
All the best.

Add a comment
Know the answer?
Add Answer to:
C++ Background information: A company has four salespeople who sell five different products. Once a day,...
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
  • 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...

  • Use a two-dimensional array to solve the following problem. A company has four salespeople (0 to...

    Use a two-dimensional array to solve the following problem. A company has four salespeople (0 to 3) who sell five different products (0 to 4). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains the following: a) The salesperson number b) The product number c) The total dollar value of that product sold that day Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that...

  • Use a two-dimensional array to solve the following problem. A company has four salespeople (1 to...

    Use a two-dimensional array to solve the following problem. A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product sold. Each slip contains the following a) The salesperson number b) The product number c) The total dollar value of that product sold that day Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that...

  • Need help with this programming please this is the instruction my teacher provided Write a C++...

    Need help with this programming please this is the instruction my teacher provided Write a C++ class called "Sales" and a main( ) function that uses the class. Also, write documentation of your project. Below is a specification of the class. . INTRODUCTION A company has four salespeople (1 to 4) who sell five different products ( to 5)1. Once a day each salesperson passes in a slip for each different type of product sold. Each slip contains: The salesperson...

  • ho, then print the message "Next flight leaves in 3 hours." 6.22 Total Sales) Use a...

    ho, then print the message "Next flight leaves in 3 hours." 6.22 Total Sales) Use a two-dimensional array to solve the following problem. A company has four salespeople (I to 4) who sell five diferent products (I to 5). Once a day, each salesperson pases in a slip for each different type of product sold. Each slip contains: a) b) c) The salesperson numbor The product number The total dollar value of that product sold that day Thus, each salesperson...

  • Java Objective: The goal of this assignment is to practice 2-dimensional ragged arrays. Background: Within a...

    Java Objective: The goal of this assignment is to practice 2-dimensional ragged arrays. Background: Within a healthy, balanced diet, a grownup needs 2,250 calories a day You will write a program to track calorie intake of a person. Assignment: Calorie intake data from a person is provided in a text file named input.txt. There are arbitrary number of double values on each line, separated by spaces. The numbers represent the number of calories consumed for meals and/or snacks on a...

  • This is in C. 4.19 (Calculating Sales) An online retailer sells five different products whose retail...

    This is in C. 4.19 (Calculating Sales) An online retailer sells five different products whose retail prices are shown in the following table: Product number Retail price $ 2.98 $ 4.50 $9.98 $ 4.49 $6.87 Write a program that reads a series of pairs of numbers as follows: a) Product number b) Quantity sold for one day Your program should use a switch statement to help determine the retail price for each product. Your program should calculate and display the...

  • Program 5 Due 10/25 C-String and Two-dimensional Array Use An input data file starts with a...

    Program 5 Due 10/25 C-String and Two-dimensional Array Use An input data file starts with a student's name (on one line). Then, for each course the student took last semester, the file has 2 data lines. The course name is on the first line. The second line has the student's grade average (0 to 100) and the number of credits for the course Sample data: Jon P. Washington, Jr. Computer Science I 81 4 PreCalculus 75 3 Biology I 88...

  • Assignment3 - Protected View - Saved to this PC ejembi jeremy X File Home Insert DesignLayout Ref...

    please can i get the answer for the 3rd question Assignment3 - Protected View - Saved to this PC ejembi jeremy X File Home Insert DesignLayout References MailingsReview View Help Tell me what you want to do PROTECTED VIEW Be careful-files from the intermet can contain vinuses. Unless you ned to edi, it's safer to stay in Protected View Enable Editing Write a program that will read all this information for last month's sales and summarize the total sales by...

  • Lab Objectives Be able to write methods Be able to call methods Be able to declare...

    Lab Objectives Be able to write methods Be able to call methods Be able to declare arrays Be able to fill an array using a loop Be able to access and process data in an array Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing...

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