Sales and Salary

I have this program and I'm stuck, the output isn't correct, unsure of what is going wrong, but I need assistance. Here is the problem with code and output. Thank you!

PROBLEM:
A company pays its salespeople on a commission basis. The salespeople each receive $200 per week plus 9 percent of their gross sales for the sales period. For example, a salesperson who grosses $5000 in sales in the period receives $200 plus 9 percent of $5000, or a total of $650. Write a program (using an array of counters and an array of accumulators) determines for each salesperson their total sales, their average sales and their salary. There are 10 salesmen for the company. The data file is "SalesPerson Sales.txt". It contains the salesperson number (1-10) followed by his sales. Each salesperson has numerous sales listed in the file.

Print out each salesman's number, their total sales, their average sales and their salary for the given period in a nice table format. I also need the total sales amount that ALL my salesmen did for this month, a company-wide total. The government has put new taxes on my company so we need to know which salesman was my lowest producer. I need to cut back on salesman for the future tax increases on my company so print out the salesman with the lowest total sales. (Again, the columns must be lined up in a nice table format with labels at the top of each column.)

PROGRAM:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

const int MAX=10;//number of salesmen

ifstream inf("sales.in");//opens the file to bring data in
ofstream out("sales.out");//opens the file to write data to

void Read(double ary[], int& a)
{
    double b;//reads the data for sales.

    while(inf >> a)//reads till end of file
    {
        double bry[MAX];
        ary[a-1]++;//reads employee ID and increases the sales.
        inf >> b;
        bry[a-1]+=b;//updates the sales
    }
}

void Write(double sales[], double transactions[], double salaries[])
{
    for(int i=0;i<MAX;i++)
	{
        out <<"\nSNumber\tTotalsales\tavgsales\tsalary\n"//displaying salary
            << setprecision(2) << fixed
            << (i+1) << "\t\t" << sales[i] << "\t\t"
            << (sales[i]/transactions[i]) << "\t\t"
            << salaries[i] << "\n";//displaying data
	}
}

void Data(double sales[], double salaries[])
{
    double lowestsales=sales[0];
	int lowestSalesIndex=0;
		for(int i=0;i<MAX;i++)//updating salaries
        {
            if(sales[i]>5000)//if sales greater than 5000 then salary is 650
            salaries[i]=650;

            else
            salaries[i]=200;//else salary is 200

            if(lowestsales>sales[i])
            {
            lowestsales=sales[i];
            lowestSalesIndex=i;
            }
        }
	out << "Salesman with lowest sales is " << (lowestSalesIndex+1);
}

int main()
{
    int counter;
    double sales[MAX];//to hold total sales of each person
    double salaries[MAX];//to hold salaries
	double transactions[MAX];//to hold no of transactions made by each sales man

	Read(sales, counter);
	Write(sales, transactions, salaries);
	Data(sales, salaries);

    inf.close();
    out.close();
    return 0;
}


OUTPUT:
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
SNumber	Totalsales	avgsales	salary
1		7139920234593805643413693101030223744215815491984164903440766951133043394181008088538266403222279660046434200032454243085299561179765241485382226542253631893468731541831144308092149941849991002651310053458554238978825795728871235446403209698248499933478912.00		-0.00		0.00

SNumber	Totalsales	avgsales	salary
2		19.00		78975628482470908703260678526723089482832677301781676680332973855437694511578868166189187529850234953215028626271870534095864609450347753833158096371535862853007956950969356868255676714241921947929321294218987877622860192661482587741596316445514474124198082549997910506171941942958762778354226719937789952.00		0.00

SNumber	Totalsales	avgsales	salary
3		19.00		92352362299531693413057455543705709890622201156353796446630990277830733183155157606220543766640358140091382402843247136510898608083298917863667958192075942568561810802959964872734441796466454119755465197890767072974421953952013603333642229762148028451151185668566107860552762291336330279614390659410982600704.00		0.00

SNumber	Totalsales	avgsales	salary
4		35.00		inf		0.00

SNumber	Totalsales	avgsales	salary
5		1.00		4156612025393202387301031811933108799255113479634613472887284183573093151062468225369615660425293101406163178589746608927047847647052871649923893292180240005057396364143269995523717210377222515147187400466856504443447504091685258357116305335727754040675504315880075111887880092902879302445704830131372032.00		0.00

SNumber	Totalsales	avgsales	salary
6		11.00		-0.00		0.00

SNumber	Totalsales	avgsales	salary
7		7.00		0.00		0.00

SNumber	Totalsales	avgsales	salary
8		59.00		inf		7375509387063864144783970993258613894893641807243891112834973312124179707833743901423069756579756265546995533505230076437397921487649583060919281064200519662383454241777778060092122738098913242540356064869623941801398059473106542703030690574086829099712512.00

SNumber	Totalsales	avgsales	salary
9		3.00		-0.00		0.00

SNumber	Totalsales	avgsales	salary
10		9.00		37409508228538826358366292007397519369987215254305676694098986449661601793515442634800886327040689145463001548312849092995888744527809936578899972231169183334510215433616139725352304871631517571719514349228532716390824276434356762056845710718987259239261346577403482420860322388264687262925638467139403776.00		0.00
Salesman with lowest sales is 5
Last edited on
You haven't SET transactions[].
What do you mean by set? I've looked over the code and there is not anything different from sales[] transactions[] or salaries[]? Also, in the function Read, bry[]=transactions[] and ary=sales[]. I've renamed them in an attempt to keep things simple. I've moved the statement in line 19 to line 16.
Last edited on
Update: I've gotten the total sales category fixed, now I just need assistance with the average and salaries column within the program. Here is the current code and output.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

const int MAX=10;//number of salesmen

ifstream inf("sales.in");//opens the file to bring data in
ofstream out("sales.out");//opens the file to write data to

void Read(double sales[], int& a)
{
    double b;//reads the data for sales.
    int transactions[MAX];
    for(int i=0;i<MAX;i++)//setting all elements in sales and transactions to 0
        {
        sales[i]=0;
        transactions[i]=0;
        }
    while(inf >> a)//reads till end of file
    {
        transactions[a-1]++;//reads employee ID and increases the sales.
        inf >> b;
        sales[a-1]+=b;//updates the sales
    }
}

void Write(double sales[], int transactions[], double salaries[])
{
    out <<"\nEmployee\tTotal Sales \tAverage Sales\tSalary\n";

    for(int i=0;i<MAX;i++)
	{
        out << setprecision(2) << fixed
            << (i+1) << "\t\t" << sales[i] << "\t\t"
            << (sales[i]/transactions[i]) << "\t\t"
            << salaries[i] << "\n";//displaying data
	}
}

void Data(double sales[], double salaries[])
{
    double lowestsales=sales[0];
	int lowestSalesIndex=0;
		for(int i=0;i<MAX;i++)//updating salaries
        {
            if(sales[i]>5000)//if sales greater than 5000 then salary is 650
            salaries[i]=650;

            else
            salaries[i]=200;//else salary is 200

            if(lowestsales>sales[i])
            {
            lowestsales=sales[i];
            lowestSalesIndex=i;
            }
        }
	out << "Salesman with lowest sales is " << (lowestSalesIndex+1);
}

int main()
{
    int counter;
    double sales[MAX];//to hold total sales of each person
    double salaries[MAX];//to hold salaries
	int transactions[MAX];//to hold no of transactions made by each sales man

	Read(sales, counter);
	Write(sales, transactions, salaries);
	Data(sales, salaries);

    inf.close();
    out.close();
    return 0;
}

OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
13

Employee	Total Sales 	Average Sales	Salary
1		5456.80		0.00		0.00
2		3065.80		0.00		0.00
3		3833.00		0.00		0.00
4		12099.00		0.00		0.00
5		100.00		0.00		0.00
6		2279.00		inf		0.00
7		675.00		0.00		0.00
8		15851.00		-0.00		73755093870638641447839709932586138942.00
9		1647.00		51.47		0.00
10		205.00		0.00		0.00
Salesman with lowest sales is 5
I believe my issue is within the Data function. Reviewing the problem I don't need those integer numbers as a declaration. They're only an example. I need to adjust that function to multiply the sales total from the individual employee by 9% to get their actual salary total. As for the average I'm dumbfounded.
Update: I've gotten the program to function normal, with the minor setback of the average being wrong. Can someone assist on finding this error? And I'm trying to include a grand total for the total of sales, but each time I attempt it's either an error or the output is 0.00.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX=10;//number of salesmen

ifstream inf("sales.in");//opens the file to bring data in
ofstream out("sales.out");//opens the file to write data to

void Read(double sales[], int& employee_number, int transactions[])
{
    double purchase;//reads the data for sales.

    for(int i=0;i<MAX;i++)//setting all elements in sales and transactions to 0
    {
        sales[i]=0;
        transactions[i]=1;
    }
    while(inf >> employee_number >> purchase)//reads till end of file
    {
        sales[employee_number-1]+=purchase;//updates the sales
        transactions[employee_number-1]++;//reads employee ID and counts the number of transactions.
    }
}

void Write(double sales[], int transactions[], double salaries[])
{
    out <<"\nEmployee\tTotal Sales \tAverage Sales\tSalary\n";

    for(int i=0;i<MAX;i++)
	{
        out << setprecision(2) << fixed
            << (i+1) << "\t\t" << sales[i] << "\t\t"
            << (sales[i]/transactions[i]) << "\t\t"
            << salaries[i] << "\n";//displaying data
	}
}

void Data(double sales[], double salaries[])
{
    for(int i=0;i<MAX;i++)//updating salaries
    {
        if(sales[i]>0)
        salaries[i]=sales[i]*.09+200;
    }
}
void Least(double sales[])
{
    double lowestsales=sales[0];
	int lowestSalesIndex=0;
	for(int i=0;i<MAX;i++)
    {
        if(lowestsales>sales[i])
        {
            lowestsales=sales[i];
            lowestSalesIndex=i;
        }
    }
    out << "Salesman with lowest sales is " << (lowestSalesIndex+1);
}
int main()
{
    int counter;
    double sales[MAX];//to hold total sales of each person
    double salaries[MAX];//to hold salaries
	int transactions[MAX];//to hold no of transactions made by each sales man

	Read(sales, counter, transactions);
	Data(sales, salaries);

	Write(sales, transactions, salaries);
    Least(sales);

    inf.close();
    out.close();
    return 0;
}

OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
Employee	Total Sales 	Average Sales	Salary
1		5456.80		181.89		691.11
2		3065.80		153.29		475.92
3		3833.00		191.65		544.97
4		12099.00	336.08		1288.91
5		100.00		50.00		209.00
6		2279.00		189.92		405.11
7		675.00		84.38		260.75
8		15851.00	264.18		1626.59
9		1647.00		411.75		348.23
10		205.00		20.50		218.45
Salesman with lowest sales is 5

CORRECT OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
13

SNumber	Totalsales	avgsales	salary
1	5456.80		188.17		691.11
2	3065.80		161.36		475.92
3	3833.00		201.74		544.97
4	12099.00	345.69		1288.91
5	100.00		100.00		209.00
6	2279.00		207.18		405.11
7	675.00		96.43		260.75
8	15851.00	268.66		1626.59
9	1647.00		549.00		348.23
10	205.00		22.78		218.45
Salesman with lowest sales is 5
Last edited on
Why do you initialise transactions to 1 (ONE) and not 0 (ZERO), as was your stated intention.


In Read() employee_number can be a local variable. There is no need for it to appear in the parameter list.


To find a grand total ... add up all sales. I can't see that anywhere in your latest code.
Hey I've gotten a little further since the last update. I believe I have figured out the code 100% minus some tweaking/formatting.

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX=10;//number of salesmen

void Read(double,int,int);
void Write(double, int, double);
void Data(double, double);
void Grand(double);
void Least(double);

ifstream inf("sales.in");//opens the file to bring data in
ofstream out("sales.out");//opens the file to write data to

void Read(double sales[], int employee_number, int transactions[])
{
    double purchase;//reads the data for sales.

    for(int i=0;i<MAX;i++)//setting all elements in sales and transactions to 0
    {
        sales[i]=0;
        transactions[i]=0;
    }
    while(inf >> employee_number >> purchase)//reads till end of file
    {
        sales[employee_number-1]+=purchase;//updates the sales
        transactions[employee_number-1]++;//reads employee ID and counts the number of transactions.
    }
}

void Write(double sales[], int transactions[], double salaries[])
{
    out <<"\nEmployee Transactions\tTotal Sales \tAverage\t\t\tSalary\n";
    for(int i=0;i<MAX;i++)
	{
        out << setprecision(2) << fixed
            << (i+1) <<" \t\t "<<transactions[i]<< "\t\t\t\t$"
            << sales[i] << "\t\t$"
            << (sales[i]/transactions[i]) << "\t\t\t$"
            << salaries[i] <<  "\n";//displaying data
	}
}

void Data(double sales[], double salaries[])
{
    for(int i=0;i<MAX;i++)//updating salaries
    {
        if(sales[i]>0)
        salaries[i]=sales[i]*.09+200;
    }
}

void Grand(double sales[])
{   
    double total;
    for(int i=0;i<MAX;i++)
    total += sales[i];
    out << "Company-Wide Total: $" << total << "\n";
}

void Least(double sales[])
{
    double lowestsales=sales[0];
	int lowestSalesIndex=0;
	for(int i=0;i<MAX;i++)
    {
        if(lowestsales>sales[i])
        {
            lowestsales=sales[i];
            lowestSalesIndex=i;
        }
    }
    out << "Salesman with lowest sales is " << (lowestSalesIndex+1);
}
int main()
{
    int counter,transactions[MAX];
    double total, sales[MAX],salaries[MAX];

	Read(sales, counter, transactions);
	Data(sales, salaries);
	Write(sales, transactions, salaries);
	Grand(sales);
    Least(sales);

    inf.close();
    out.close();
    return 0;
}

OUTPUT:
1
2
3
4
5
6
7
8
9
10
11
12
13
Employee Transactions	Total Sales 	Average			Salary
1 		 29				$5456.80		$188.17			$691.11
2 		 19				$3065.80		$161.36			$475.92
3 		 19				$3833.00		$201.74			$544.97
4 		 35				$12099.00		$345.69			$1288.91
5 		 1				$100.00		    $100.00			$209.00
6 		 11				$2279.00		$207.18			$405.11
7 		 7				$675.00		    $96.43			$260.75
8 		 59				$15851.00		$268.66			$1626.59
9 		 3				$1647.00		$549.00			$348.23
10 		 9				$205.00		    $22.78			$218.45
Company-Wide Total: $45211.60
Salesman with lowest sales is 5

On line 67, why is the +1 necessary? I know if I take it out the output will be wrong, but why is it needed?
Last edited on
Also, can you explain why if I switch function calls around within the int main why the data is changed on the outfile? Is it because if a certain function is called before another, that the data isn't transferred effeciently?
CodeNovice01 wrote:
On line 67, why is the +1 necessary?

Line 67 says (at the moment)
int lowestSalesIndex=0;
so it is impossible to see what you are alluding to. Various "+1" and "-1" appear in your code to compensate for the fact that your salesmen count from 1 but array indices run from 0.


CodeNovice01 wrote:

why if I switch function calls around within the int main

You call 5 functions in main. Which particular ones are you switching around?



why the data is changed on the outfile?

Since we haven't got the input file to run your code, please tell us WHICH data is changed "on the outfile".



Lastchance wrote:
In Read() employee_number can be a local variable. There is no need for it to appear in the parameter list.

You continue to ignore this.



You don't initialise total in routine Grand().



1
2
3
    inf.close();
    out.close();
    return 0;

None of these lines are necessary.



Rather than running with several parallel arrays and a preset number of salesman you would be better with a vector of structs.






Topic archived. No new replies allowed.