Need help making additions in c++11

Pages: 12
Fixed line 25

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
You haven't fixed the errors I pointed out to you via IM. Please try and correct all compile errors before posting your code. If you don't understand why you getting certain compile errors, then say so.

Line 23: Extraneous }

Line 36: range is undefined.

Line 70: bucket is undefined

The reason why you're printing all zeroes is that you're never updating the range array defined at line 52.

I told you before, put the range array in the class and make lines 77-83 a function member of the class.

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
//  salesreport.h
#include <iostream>
using namespace std;

class SalesReport
{   double  grossSales;
    int range[11];      //  = {0};  with C++11

public:
    SalesReport()
    {   setGrossSalesEqual(0);     
        for (int i=0; i<11; i++)    //  Initialize the old way
            range[i] = 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 bucket;
        if (salary>=1000)
        {   bucket=10;
        }
        else
        {   bucket=salary/100;
        }
        range[bucket]++;
    }
    
    void PrintRanges () const
    {   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;
        }
    }
    
};

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

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

int main()
{   SalesReport theSales;   //  No need for three instances
    double sales;
    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;                   
        theSales.salRange(salary);  
        cout << "\nEnter sales in dollars (-1 to end): ";
        cin >> sales;
    }  
    theSales.PrintRanges ();
    return 0;
}



Last edited on
So put int range[11]={0} in SalesReport class?
Hello jlin55

So put int range[11]={0} in SalesReport class?

Yes.

Andy
Topic archived. No new replies allowed.
Pages: 12