C++ issue with my project

I am having issues with my code in order to work. I have a cpp and a .h class
that I need to interact.
prompt:
One large chemical company pays its salespeople on a commission basis. The salespeople each receive $200 per week plus 9 percent of their gross sales for that week. For example, a salesperson who sells $5000 worth of chemicals in a week receives $200 plus 9 percent of $5000, or a total of $650. Develop a C++ program that uses a while statement to input each salesperson’s gross sales for last week and calculates and displays that salesperson’s earnings. Process one salesperson’s figures at a time.Your program should have one member data named grossSales.
Your program should have a constructor, a get and a set member function for the data. The result salaries must be displayed in 2 decimal places.
sample run:
Enter sales in dollars:(-1 to end): 5000 //input
Salary is : 650.00 //output
Enter sales in dollars:(-1 to end): 5678
Salary is: 711.02

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
  #include <iostream>

#include "Sales.h"

int getmember ()

{
   using std::cout;
   using std::cin;

cout<<"Enter sales In dollar(-1to end)";
int grosssales;
cin>>grosssales; //input

return grosssales;

}

int setmember (int grosssales)
{
   using std::cout;
   using std::cin;
   using std::endl;
   
   int Salary=0;//starts at 0

int salesCounter=0;
while(grosssales>=1)
{
   if(grosssales==-1)
   break; //terminate if type -1
Salary=200+(0.09*grosssales);

salesCounter++;

cout<<"Salary is "<<Salary<<endl;//outputs the salary


}
return Salary;
}

int main()
{
   //calling methods


setmember(getmember());
}

.h:
class setmember
public:
{

unsigned int salesCounter=-1;//counter 



int getmember();
int grosssales;//declaration

int setmember(getmember()); //error: 'getmember is not a type', 'return type specification for constructor invalid
};
Last edited on
Hello jlin55,

There are several problems with your code.

In the "Sales.h" file I would call the class "member" instead of "setmember". "setmember" is a better choice for a member name of a class function. "public:" should be after the opening brace not before and there is no "private:" section or member variables. Done correctly "setmember(getmember())" may work, but not only does it not work the way you are using it because of the way you have defined your functions.

At the beginning of your program you define "getmember" and "setmember" functions. Are the functions part of the class or separate?

The statements using std::cout;, using std::cin; and using std::endl; only need to
be used once per file and are best placed after "#include Sales.h". I would avoid using "using namespace std;" and using statements and just get use to typing "std::cout..." etc. It goes a long way towards avoiding conflicts in your program. And after you re finished with school these are not used much.

I will be worth your time to read up on classes:
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/templates/

I will work on your program in the morning and see what I can come up with.

Hope that helps,

Andy


What about this set up? I am also stuck with this one a bit.

main.cpp:
#include <iostream>
#include "Sales.h"
using namespace std;
using std::cout;
using std::cin;
using std::endl;
int main()
{
double salary, sales;
Sales mySales;
//double commision=0.09;//rate given


cout<<"Enter sales In dollar(-1 to end)";
cin>>sales; //input

// write the loop to get more values
while(sales!=-1)
{

}
mySales.setGrossSales( sales );
salary = mySales.findSalary();

cout<<"Salary is "<<salary<<endl;//outputs the salary

}


sales.h:
class Sales
{
public:

// write constructor here

double getGrossSales()
{
return grosssales;
}

void setGrossSales( double sales)
{
while(sales!=-1)
{
grosssales=sales;
}
// write codes here
}

double findSalary()
{

salary=(0.09*grosssales)+200;
return salary;
// calculate and return the salary using grosssales
}

private:
double grosssales;
};
Hello jlin55,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

The "Sales" class is better, but not quite there yet. You have a private variable of grosssales, but who does it belong to? You also need to define a constructor and destructor as your comment says. Not sure if the "Sales.h" file is the best place to put line 11, but it is my first thought. I would also write that line as: constexpr double commision=0.09;//rate given or just make it "const" if needed. I would also check your spelling of "commission" before you spell it correctly and have a problem trying to figure out where you went wrong.

In main lines 14 - 21 are a good start, but you need some code in the while loop to process your input starting with asking for a name then processing what was entered ending the loop by asking for the sales input where if it is not -1 you will process the new information. My idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cout << "Enter sales In dollar(-1 to end)";
cin >> sales; //input

// write the loop to get more values
// If sales > -1 enter the loop

while (sales != -1)
{
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires the limits header file. Used after std::cin >> var; and before a call to std::getlineto clear the \n fromthe input buffer.
    std::cout << "\nEnter name: ";
    std::getline(std::cin, name);  // <--- Where name is defined as a std::string.

    // Code to process the input
    // Store in vector

    cout << "Enter sales In dollar(-1 to end)";
    cin >> sales; //input
}

The concept is good, but I have not tested this yet.

You will need to define a vector std::vector<Sales> anyNameYouLike;.

The beginning of main would be this:

1
2
3
4
#include <iostream>
#include <string>
#include <vector>
#include <limits>  // <--- for the std::cin.ignore(...). 


I will work with your new changes and see if I find anything else.

Hope that helps,

Andy
Hello jlin55,

I wanted to mention, until I was delayed by breakfast, by using a vector or even an array you can pass the vector to a "Print" or "Display" function to print out a report. This is why I am thing of storing "name", "grossSales" and "grrossPay" in the class for future processing in functions like "Display" or a "Find" type function.

Hope that helps,

Andy
I don't need the name of the person to keep track. This is what the program should look like when it runs.

Enter sales in dollars:(-1 to end): 5000 //input
Salary is : 650.00 //output
Enter sales in dollars:(-1 to end): 5678
Salary is: 711.02
Here is what I have so far with the second setup: I was able to get the user to enter in the the sales, but I need help making sure it calculates and shows the salary for that, then enter another sale, and show the salary for that and so on.


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
main.cpp:
#include <iostream>
#include <string>
#include <vector>
#include <limits> 
#include "Sales.h"
using namespace std;
using std::cout;
using std::cin;
using std::endl;
int main()
{
	double salary, sales;
    Sales mySales;
    
    
    
	cout<<"Enter sales In dollar(-1 to end)";
    cin>>sales; //input

// write the loop to get more values
while(sales!=-1)
{
		cout<<"Enter sales In dollar(-1 to end)";
    cin>>sales; //input
}
        mySales.setGrossSales( sales );
        salary = mySales.findSalary();

        cout<<"Salary is "<<salary<<endl;//outputs the salary

}


Sales.h:
class Sales
{
  public:

   // write constructor here

  double getGrossSales()
  {
  	   return grosssales;
  }

  void setGrossSales( double sales) 
  {
	while(sales!=-1)
	{
		grosssales=sales;
	}
  	//  write your codes here
  }
  
  double findSalary()
  {
  	int salary;
  	 salary=(0.09 * grosssales) + 200; 
  	 return salary;
  	  // calculate and return the salary using grosssales
  }

  private:
  	double grosssales;
};

Last edited on
Hello jlin55,

My misunderstanding, the use of the class trough me off. For what you are looking for the class is over kill unless the point is to learn about classes. Simply put this is all you need:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
constexpr double commission{ 0.09 }; // <--- Added to main for this example.

std::cout << "\n Enter sales In dollar(-1 to end) " << std::endl;
std::cout << "\n Sales: ";
std::cin >> sales; //input

while (sales != -1)
{
	std::cout << "\n Total Pay is $" << sales * commission + 200 << std::endl;

	std::cout << "\n Enter sales In dollar(-1 to end) " << std::endl;
	std::cout << "\n Sales: ";
	std::cin >> sales; //input
}


or if you use the class:

1
2
3
4
5
6
7
8
9
10
11
12
	std::cout << "\n Enter sales In dollar(-1 to end) " << std::endl;
	std::cout << "\n Sales: ";
	std::cin >> sales; //input

	while (sales != -1)
	{
		std::cout << "\n Total Pay is $" << mySales.SetGrossSales(sales) << std::endl;

		std::cout << "\n Enter sales In dollar(-1 to end) " << std::endl;
		std::cout << "\n Sales: ";
		std::cin >> sales; //input
	}

in this example I changed the "SetGrossSales" function to return a double and in the "SetGrossSales" I added "return FindSalary();" so I could use the single cout statement.

The "SetGrossSales" function the while loop is not needed. It would work better this way:

1
2
3
4
5
double Sales::SetGrossSales(double sales)
{
	grosssales = sales;
	return FindSalary();
}


In "FindSalary()" you have the line "salary=(0.09 * grosssales) + 200;". A better practice would be to place "constexpr double commission{0.09};" at the top of the file and write the line as salary=(commission * grosssales) + 200;". This way you do not have to search the entire file to find "0.09" to change it. You can find it in one place at the top of the file. It does not seem like much now, but in a bigger program you will see the difference.

Hope that helps,

Andy
I was able to get the the user to input, but doesn't show the salary once calculated. In the setGrossSales() I made 'return findSalary()' as a change. I am also having trouble making salaries be in 2 decimal places. Any suggestions with this
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
class Sales
{
  public:

   // write  constructor here

  double getGrossSales()
  {
        return grosssales;
  }

//setter method 
  void setGrossSales( double sales) 
  {
    if(sales!=-1)
{
        grosssales=sales;
     
    }
   
   
      //  write  codes here
  }
  
  double findSalary()
  {
   int salary;
      salary =(0.09 * grosssales) + 200; 
      return salary;
       // calculate and return the salary using grosssales
  }

  private:
      double grosssales;
};

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <limits> 
#include "sales.h"
using namespace std;
using std::cout;
using std::cin;
using std::endl;
int main()
{
    double salary, sales;
    Sales mySales;
    
    
    
    cout<<"Enter sales In dollar(-1 to end): ";
    cin>>sales; //input

// write the loop to get more values
while(sales!=-1)
{
        cout<<"Enter sales In dollar(-1 to end): ";
    cin>>sales; //input

        mySales.setGrossSales( sales );
        salary = mySales.findSalary();

        cout<<fixed<<setprecision(2)<<"Salary is "<<salary<<endl;//outputs the salary, hoping this works with precision of 2 digits.
       
 }       

}
Last edited on
Hints:

main.cpp:
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
// One large chemical company pays its salespeople on a commission basis. 
// The salespeople each receive $200 per week plus 9 percent of their gross 
// sales for that week. For example, a salesperson who sells $5000 worth 
// of chemicals in a week receives $200 plus 9 percent of $5000, or a total 
// of $650. Develop a C++ program that uses a while statement to input each 
// salesperson’s gross sales for last week and calculates and displays that 
// salesperson’s earnings. Process one salesperson’s figures at a time.
// Your program should have one member data named grossSales.
// Your program should have a constructor, a get and a set member function 
// for the data. The result salaries must be displayed in 2 decimal places.
// sample run:
// Enter sales in dollars:(-1 to end): 5000 //input
// Salary is : 650.00 //output
// Enter sales in dollars:(-1 to end): 5678
// Salary is: 711.02
#include <iomanip>
#include <iostream>
#include <limits> 
#include <string>
#include "Salesperson.h"

void waitForEnter();

int main()
{
    // "...uses a while statement to input each salesperson’s gross sales for 
    // last week and calculates and displays that salesperson’s earnings."
    // 'while' is not the best advice: since the loop must run at least once, 
    // a 'do-while' loop would be the best choice. Anyway:
    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);
        if(week_sales == -1) { break; }
        Salesperson spers(week_sales);
        std::cout << "Your week salary is " << std::fixed << std::setprecision(2)
                  << spers.findSalary() << '\n';
    }

    waitForEnter();
    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}


Salesperson.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef MY_SALESPERSON
#define MY_SALESPERSON


class Salesperson {
public:
    Salesperson(double gross_sales_arg = 0.0);
    double getGrossSales();
    void setGrossSales(double sales);
    double findSalary();

private:
    double gross_sales {0.0};
};


#endif // MY_SALESPERSON 


Salesperson.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "Salesperson.h"

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

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


void Salesperson::setGrossSales(double sales)
{ if(sales != -1) { gross_sales = sales; } }


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


Here is what I have so far:
I was able to get the user input the sales, but it does not show the salary right after, but if the user would then input the next it does show the salaries. Any way you can fix this?

the run of my program:
Enter sales(-1 quit): 5000
Enter sales (-1 quit):5678
Salary is 711.00 //should be 711.02
Also I am having trouble with the precision.
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
main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <limits> 
#include "sales.h"
using namespace std;
using std::cout;
using std::cin;
using std::endl;
int main()
{
    double salary, sales;
    Sales mySales;
    
    
    
    cout<<"Enter sales In dollar(-1 to end): ";
    cin>>sales; //input

// write the loop to get more values
while(sales!=-1)
{
        cout<<"Enter sales In dollar(-1 to end): ";
    cin>>sales; //input

        mySales.setGrossSales( sales );
        salary = mySales.findSalary();

        cout<<std::fixed<<std::setprecision(2)<<"Salary is "<<salary<<endl;//outputs the salary
        //cout;
    }   

}
 
sales.h:
class Sales
{
  public:

   // write your constructor here

  double getGrossSales()
  {
        return grosssales;
  }
//this function is the setter
  void setGrossSales( double sales) 
  {
    
   if(sales!=-1)
   {
    
        grosssales=sales;
      
}
   
      //  write your codes here
  }
  
  double findSalary()
  {
   int salary;
      salary =(0.09 * grosssales) + 200; 
      return salary;
       // calculate and return the salary using grosssales
  }

  private:
      double grosssales;
};

Topic archived. No new replies allowed.