Need help making additions in c++11

Pages: 12
I need help trying to adjust my program to look like the sales report. Any suggestions on how to approach this.

Prompt:
Use a single-subscripted array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000 or a total of $650. Write a program (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):
a) $200-$299
b) $300-$399
c) $400-$499
d) $500-$599
e) $600-$699
f) $700-$799
g) $800-$899
h) $900-$999
i) $1000 and over
Summarize the results in tabular format.
Have a .cpp and a Salesreport.h that contains the Sales.

Requirements:

1. Create a class named SalesReport with the array of counters as the class member data.
2. Write an integer function that returns the salary with the gross sales as the function argument.
3. Write a void function that takes the salary as the function argument and increase the appropriate array elements.

Sample Run:
Enter a sales amount: (negative to end):1000
Enter a sales amount: (negative to end):3000
Enter a sales amount: (negative to end):3000
Enter a sales amount: (negative to end):5000

Range Number
$200-299 1
$300-399 0
$400-499 2
$500-599 0
$600-699 1


Here is what I have so far: A main.cpp and a Sales.h

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
  Main.cpp
#include <iomanip>
#include <iostream>
#include <limits> 
#include <string>
#include "Sales.h"
using namespace std;
using std::cout;
using std::cin;
using std::endl;

Salesperson::Salesperson(double gross_sales_arg)
    : grossSales {gross_sales_arg}
    {}

double Salesperson::getGrossSales() 
{ 
   return grossSales;
}

//setter method
void Salesperson::setGrossSales(double sales)
{ 
   if(sales != -1)
    { 
       grossSales = sales; 
    } 
}


// calculate and return the salary using gross_sales
double Salesperson::findSalary()
{ 
   return (0.09 * grossSales) + 200; //salary calculation
}


void waittoEnter();

int main()
{
    double week_sales = 0.0;
    while(week_sales != -1)
    {
        std::cout << "Please enter sales in dollars (-1 to end): ";
        std::cin >> week_sales; //input
        std::cin.ignore(1); //helps to ignore next line
        if(week_sales == -1)
         {
            break; 
         }
        Salesperson spers(week_sales);//salesperson object
        std::cout << "salary is " << std::fixed << std::setprecision(2)
                  << spers.findSalary() << '\n';//precision requirement
    }

    waittoEnter();
    return 0;
}
//this function makes sure to allow enter to continue
void waittoEnter()
{
    std::cout << "\nPress enter to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
Sales.h
#ifndef SALES
#define SALES


class Salesperson {
public:
//calling methods
    Salesperson(double gross_sales_arg = 0.0);//type cast
    double getGrossSales();
    void setGrossSales(double sales);
    double findSalary();

private:
    double grossSales= 0.0;
};


#endif // SALESPERSON  
Last edited on
Use a single-subscripted array

I see no such array. You need an array to count the number of sales people in each hundred dollar range.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int range[11] = {0};
int bucket;
int salary;

  //  Within the input loop
  salary = findSalary();
  if (salary >= 1000)     
      bucket = 10; 
  else
      bucket = salary % 100;
  range[bucket]++;

  // After the input loop
  for (int i=2; i<=10; i++)
  { if (i == 10)
         std::cout << "$1000 and over: " << bucket[i] << std::endl;
     else
         std::cout << "$" << i << "00-$" << i << "99: " << bucket[i] << std::endl;
  }


Requirement 1. Create a class named SalesReport

Your class is not named SalesReport.

Requirement 2. Write an integer function that returns the salary.

Line 75 getGrossSales returns a double, not an integer.
Last edited on
Hello jlin55,

AbstractionAnon has mentioned the big points that I would have. The only things I can add are:

Line 52 "Salesperson spers", or what should be called "SalesReport", should be moved outside the while loop because with "grossSales" being defined as an array you only need one instance of the class defined. Line 52 should only call a set function of the class to put the the final salary into the array. I am think that the class's private variables should contain two new variables to keep track of which position in the array to store new information and the second variable to keep track of how many element of the array re used.

The set function can call the "findSalary" function before the final value is stored in the array.

Line 53 might work better as a "Display" function of the class. Just my thoughts for now.

Hope that helps.

Andy
How about this setup instead:
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
main.cpp
#include <iostream>
#include <iomanip>
#include "Sales.h"
using namespace std;

int main()
{
    
	Sales mySales;
        
     double sales, salary;
     
     cout << "Enter sales in dollars (-1 to end): ";
     cin >> sales;         
    
     while (sales != -1)
     {           
           mySales.setGrossSales(sales);
		   salary = mySales.findSalary();
           cout << setprecision(2) << fixed;
           cout << "Salary is: " << salary << endl;
           
           cout << "\nEnter sales in dollars (-1 to end): ";
           cin >> sales;
     }
    
return 0;
}


Sales.h:
#include <iostream>
using namespace std;

class Sales
{
      public:
    
		Sales()
		{
		    setGrossSales(0);     
		}
		
		void setGrossSales (double sale)
		{
		    grossSales = sale;
		}
		
		double getGrossSales()
		{
		    return grossSales;
		}
		
		double findSalary()
		{
		     return 200 + 0.09 * grossSales;          
		} 
		            
      private:
        double grossSales;
              
};
Last edited on
Your program still doesn't meet requirements 1 or 2.

I gave you the code to accumulate the counts of and report each hundred dollar range. Why didn't you use it?



Last edited on
AbstractionAnon, do I put the code you gave me in my main.cpp or in my SalesReport.h?
I gave you code. Code should go in a .cpp file.
Is this the setup that I should go with?
Here is what I have so far:
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
main.cpp
#include <iostream>
#include <iomanip>

#include "SalesReport.h"
using namespace std;

int main()
{
    
    Salesreport theSales;
 double sales, salary;
     
     cout << "Enter sales in dollars (-1 to end): ";
     cin >> sales;         
    while(scanf("%d", sales)!=EOF)
    {
     while (sales != -1)
     {           
           theSales.setGrossSalesEqual(sales);
           salary = theSales.calculateSalary();
           cout << setprecision(2) << fixed;
           cout << "Salary is: " << salary << endl;
           
           cout << "\nEnter sales in dollars (-1 to end): ";
           cin >> sales;
     }
     printf("Employees in salary range:\n");
     for(int i=2;i<10;i++)
     {
        printf("$d00-$%d99:%d\n",i,i,count[i]);
     }
     printf("$1000 and over:%d\n",count[10]);
  }


     ///////////////////////////////
   //
   int range[11] = {0};//array for to count number of sales people in each hundred dollar range
int bucket;


  //  Within the input loop
  salary = calculateSalary();
  if (salary >= 1000)   
  {  
      bucket = 10; 
   }
  else {

      bucket = salary % 100;
  range[bucket]++;
 }
  // After the input loop
  for (int i=2; i<=10; i++)
  { 
     if (i == 10)
     {
         std::cout << "$1000 and over: " << bucket[i] << std::endl;
      }
     else {
         std::cout << "$" << i << "00-$" << i << "99: " << bucket[i] << std::endl;
      }
      
  }
return 0;
}

Salesreport.h:
#include <iostream>
using namespace std;
class Salesreport
{
   public:

//determine and increment corresponding array element for salary range.
double findRange() const {
   if(salary>=1000)
   {
      count[10]++;
      else if(salary>=200)
      {
         count[salary/100]++;
      }
   }
   
   Salesreport()
        {
           setGrossSalesEqual(0);     
        }
        
        void setGrossSalesEqual (double sale)
        {
           grossSales = sale;
        }
        
        double getGrossSales()
        {
           return grossSales;
        }
        
        double calculateSalary()
        {
            return 200 + (0.09 * grossSales);          
        } 
                   
      private:
        double grossSales;
   
///////////////////////
};



Last edited on
Lines 33,33: Why are you using printf (a C function) in a C++ program? These lines duplicate 59 and 62.

Line 43-53: The comment explicitly said "within the input loop". These lines are not within the input loop.

Line 72: Your capitalization does not match requirement 1.

Lines 77-85: What is this function doing? It won't compile.

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
//  salesreport.h
#include <iostream>
using namespace std;
class SalesReport
{   double  grossSales;

public:
    SalesReport()
    {   setGrossSalesEqual(0);     
    }
        
    void setGrossSalesEqual (double sale)
    {   grossSales = sale;
    }
        
    double getGrossSales()
    {   return grossSales;
    }
        
    int calculateSalary()   // Requirement 2 says this should be an int function
    {   return (int)(200 + (0.09 * grossSales));          
    }     
};

//  main.cpp
#include <iostream>
#include <iomanip>

//  #include "SalesReport.h"
using namespace std;

int main()
{   SalesReport theSales;
    double sales;
    int range[11] = {0};//array for to count number of sales people in each hundred dollar range
    int bucket;   
    int salary;  
    
    cout << "Enter sales in dollars (-1 to end): ";
    cin >> sales;         
    while (sales != -1)
    {   theSales.setGrossSalesEqual(sales);
        salary = theSales.calculateSalary();
        cout << setprecision(2) << fixed;
        cout << "Salary is: " << salary << endl;                   
        if (salary >= 1000)   
            bucket = 10; 
        else 
            bucket = salary / 100;
        range[bucket]++;
        cout << "\nEnter sales in dollars (-1 to end): ";
        cin >> sales;
    }
    for (int i=2; i<=10; i++)
    {   if (i == 10)
            cout << "$1000 and over: " << range[i] << endl;
        else 
            cout << "$" << i << "00-$" << i << "99: " << range[i] << endl;
    }      
    system ("pause");
    return 0;
}

Last edited on
AbstractionAnon,

What about requirement #3?
Last edited on
range[] can be moved into the class.

Lines 46-50 become the void function.

I's also make lines 54-59 into a function. i.e. displayTable ()
so is this what you meant to make a void function in lines 46-50? Do I put it in the Salesreport?
1
2
3
4
5
6
7
8
9
10
11
12
13
void salRange(salary)
{
if (salary>=1000)
{
   bucket=10;
}
else
{
bucket=salary/100;
range[bucket]++;
}
}



Line 2: You will need bucket as a local variable.

Yes, it goes in the SalesReport class.
Hello jlin55,

What happened??

You had a program that was near being completer to a whole new format mixing C code in a C++ program that will not even compile.

Lint 71 is giving me a problem with my compile. Lines 80 and 83, the compiler does not know if the "count" you are trying to use is the one in the "std::" name space or a variable that I have not found a definition for yet. this is a good example of why not to use "using nanespace std;". This can be fixed, but it will not follow the requirements of only taking one parameter.

You continue to have functions of the class return a double when they need to return an int.

When you figure out which way you want to write the program let me know and we can work something out.

Andy
Is this what you meant?
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
//  salesreport.h
#include <iostream>
using namespace std;
class SalesReport
{   double  grossSales;

public:
    SalesReport()
    {   setGrossSalesEqual(0);     
    }
        
    void setGrossSalesEqual (double sale)
    {   grossSales = sale;
    }
        
    double getGrossSales()
    {   return grossSales;
    }
        
    int calculateSalary()   // Requirement 2 says this should be an int function
    {   return (int)(200 + (0.09 * grossSales));          
    }     
void salRange( int salary)
{
int range[11]={0};
int bucket;
if (salary>=1000)
{
   bucket=10;
}
else
{
bucket=salary/100;
range[bucket]++;
}
}

};

//  main.cpp
#include <iostream>
#include <iomanip>

//  #include "SalesReport.h"
using namespace std;

int main()
{   SalesReport theSales;
    double sales;
    int range[11] = {0};//array for to count number of sales people in each hundred dollar range
  //  int bucket;   
    int salary;  
    
    cout << "Enter sales in dollars (-1 to end): ";
    cin >> sales;         
    while (sales != -1)
    {   theSales.setGrossSalesEqual(sales);
        salary = theSales.calculateSalary();
        cout << setprecision(2) << fixed;
        cout << "Salary is: " << salary << endl;                   
	salRange(salary);
        cout << "\nEnter sales in dollars (-1 to end): ";
        cin >> sales;
    }
    for (int i=2; i<=10; i++)
    {   if (i == 10)
            cout << "$1000 and over: " << range[i] << endl;
        else 
            cout << "$" << i << "00-$" << i << "99: " << range[i] << endl;
    }      
    
    return 0;
}






Last edited on
Line 25: I said to make range a member variable of the class. This array is uninitialized.

Line 50: You don't want the array here.

Lines 67-72: I said to make this a member function of your class. That's the only way you will have addressibility to range if range is a private member.



So in lines 67-72 do I put those into my salRange()? I'm still confuse about that part.
Here is what I have so far. I'm stuck with these errors in .h file
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
//  salesreport.h
#include <iostream>
using namespace std;
class SalesReport
{   double  grossSales;

public:
    SalesReport()
    {   setGrossSalesEqual(0);     
    }
        
    void setGrossSalesEqual (double sale)
    {   grossSales = sale;
    }
        
    double getGrossSales()
    {   return grossSales;
    }
        
    int calculateSalary()   // Requirement 2 says this should be an int function
    {   return (int)(200 + (0.09 * grossSales));          
    }     
}

void salRange(salary)
{
int bucket;
if (salary>=1000)
{
   bucket=10;
}
else
{
bucket=salary/100;
range[bucket]++;
}
}

};

//  main.cpp
#include <iostream>
#include <iomanip>

//  #include "SalesReport.h"
using namespace std;

int main()
{   SalesReport theSales,money, display;
    double sales;
     int range[11]={0};
    int salary;  
    
    cout << "Enter sales in dollars (-1 to end): ";
    cin >> sales;         
    while (sales != -1)
    {   theSales.setGrossSalesEqual(sales);
        salary = theSales.calculateSalary();
        cout << setprecision(2) << fixed;
        cout << "Salary is: " << salary << endl;                   
     
	money.salRange(salary);  //
        cout << "\nEnter sales in dollars (-1 to end): ";
        cin >> sales;
    }
  for (int i=2; i<=10; i++)
    {  
        if (i == 10)
            cout << "$1000 and over: " << range[i] << endl;
        else 
            cout << "$" << i << "00-$" << i << "99: " << range[i] << endl;
    }

    return 0;
}



Last edited on
Hello jlin55,

Where is the array "range" defined? I cannot find it.

Lines 2 and 3 of the header file are not needed. Header guards are a good idea and something you should get use to using.

Line 58 defines three objects of "SalesReprot", but only the first is needed.

Hope that helps,

Andy

I was able to compile the program correctly, but counting all the salary made for each person are not counted at all no matter how high the sales that I entered in. Any suggestions guys?
Here is the new update on my 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
74
75
76
77
78
79
80
81
82
83
84
85
86
//  salesreport.h
#include <iostream>
using namespace std;
class SalesReport
{   double  grossSales;

public:
    SalesReport()
    {   setGrossSalesEqual(0);     
    }
        
    void setGrossSalesEqual (double sale)
    {   grossSales = sale;
    }
        
    double getGrossSales()
    {   return grossSales;
    }
        
    int calculateSalary()   // Requirement 2 says this should be an int function
    {   return (int)(200 + (0.09 * grossSales));          
    }     
}
//this method is where I'm am trying to get it access to main class below.
void salRange( int salary)
{
int bucket;
if (salary>=1000)
{
   bucket=10;
}
else
{
bucket=salary/100;
}
range[bucket]++;

}

};

//  main.cpp
#include <iostream>
#include <iomanip>

//  #include "SalesReport.h"
using namespace std;

int main()
{   SalesReport theSales,money, display;
    double sales;
     int range[11]={0};
    int salary;  
    //int bucket;// do I need this there?
    cout << "Enter sales in dollars (-1 to end): ";
    cin >> sales;         
    while (sales != -1)
    {   theSales.setGrossSalesEqual(sales);
        salary = theSales.calculateSalary();
        cout << setprecision(2) << fixed;
        cout << "Salary is: " << salary << endl;                   
     
       //  if(salary>=1000)
         // {
       //       bucket=10;
      //     }
      //     else
       //      {
          //       bucket=salary;
                    range[bucket]++;
                 //}
	money.salRange(salary);  //
         //The code for if and else statements is where I made them into a function in the salesreport class
        cout << "\nEnter sales in dollars (-1 to end): ";
        cin >> sales;
    }
  for (int i=2; i<=10; i++)
    {  
        if (i == 10)
            cout << "$1000 and over: " << range[i] << endl;
        else 
            cout << "$" << i << "00-$" << i << "99: " << range[i] << endl;
    }
    return 0;
}

Last edited on
How does line 25 even compile? You don't specify the type of the salary parameter.
Pages: 12