calculate amount of salary received based on position level

Here the question.

A company owned by First Avenue has a salary system based on position level. Employee salary is being paid on a weekly basis. The company has 30 employees, which comprises of a manager (fixed salary), cooks (salary on hourly basis), salesperson (fixed salary and sales commission). Below shows the details of salary payment for each position level.

1) Position level : Manager
Salary paid per week : Fixed salary

2) Position level : Cooks
Salary paid per week : First 30 hours - fixed rate every hour. The next following hours, overtime rate at 1.5% of the fixed rate hourly

3) Position level : Salesperson
Salary paid per week : $300 plus commission 8.5% of the total sales per week.

Calculate the amount of salary received for each employee based on their position level in the company.

Given is the input, output, solution/formula, and constraint for the above problem :

Inputs:
*fixed input:
- fixed salary for a manager
- fixed rate for cooks
- basic salary for salesperson
- commission rate

*user’s input:
- position category
- total working hours for cooks
- total sales for salesperson

Outputs:
- position category
- total income

Solution/Formula:
1) Income for a manager is fixed

2) Income for cooks is calculated by multiply total working hours with fixed rate:

2.1 if total working hours less than or equal 30 hours,
Total income = total working hours * fixed rate
2.2 if total working hours more than 30 hours
Total income = 30 * fixed rate + total working hours – 30 * 1.5 * fixed rate

3) Total income for a salesperson = $300 + commission rate * total sales per
week


Constraints:
- total working hours can not exceed than 168 hours
- total working hours must be entered for cooks
- total sales must be entered for salesperson

so, have to write a C++ program to calculate.
Last edited on
Hi,

so, have to write a C++ program to calculate.


Well go ahead then - make a start :+)

Show us your code then we can help from there. Make sure to use the code tags - use the <> format button.

If you have any compile errors, then post them in full verbatim.

There are plenty of people to help, you just have to show some effort first.

Cheers
sorry, here is the code that i have done :

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
#include <iostream>
using namespace std;

int main(){
	double managersalary, cooksfixedrate, salespersonsalary, commissionrate, cookstotalworkinghours, salespersontotalsales, totalincome;
	char position, manager, cooks, salesperson;

	managersalary = 1000.00;
	cooksfixedrate = 15.00;
	salespersonsalary = 300.00;
	commissionrate = 0.085;
	position = manager, cooks, salesperson;

	cout << "Position category : ";
	cin >> position;

	if (position = manager)
	{
		totalincome = managersalary;
	}

	else if (position = cooks)
	{
		cout << "input total working hours : ";
		cin >> cookstotalworkinghours;
		if (cookstotalworkinghours <= 30)
		{
			totalincome = cookstotalworkinghours * cooksfixedrate;
			cout << "total income : " << totalincome;
		}
		else if (cookstotalworkinghours > 30);
			totalincome = (30 * cooksfixedrate) + cookstotalworkinghours - (30*1.5*cooksfixedrate);
			cout << "total income : " << totalincome;
	}

	else if (position = salesperson)
	{
		cout << "total sales per week : ";
		cin >> salespersontotalsales;

		totalincome = 300 + (commissionrate * salespersontotalsales);
		cout << "total income : "; <<totalincome;
	}

	return 0;

}



these are the errors :
Error 1 error C2143: syntax error : missing ';' before '<<' line 29
Error 4 error C2143: syntax error : missing ';' before '<<' line 33
Error 3 error C2146: syntax error : missing ';' before identifier 'totalincome' line 32
Error 2 error C2181: illegal else without matching if line 31
6 IntelliSense: expected an expression line 42


is my code is correct to calculate the salary ?
Last edited on
On line 28, else if (cookstotalworkinghours > 30);

Get rid of the semicolon.

On line 42,
cout << "total income : "; <<totalincome;

Get rid of the first semicolon. It should look like this :

cout << "total income : " <<totalincome;

On line 12,

position = manager, cooks, salesperson;

You did not initialized the variables "manager", "cooks" and "salesperson"

You may get a compiler error saying, "uninitialized local variable 'whatever variable' used."

I hope this helps.
thanks for your reply..

can i know how to initialize the variables "manager", "cooks" and "salesperson" ?

i try to find how to initialize it but can't understand...
You typed your position to a "char", which means it's only going to take exactly one keyboard stroke other than whitespaces (aka 'n', 'a', '~', '8'). It looks like you also want your position variable to be of type string so it can match the various types of jobs.

Keep in mind, anything that's not in single or double quotes will not count as a character or string, which means the compiler thinks salesperson is a variable.

Hint: salesperson vs. "salesperson"
Hi,
Sorry for my late reply, I have been busy trying to rent a house :+)

Here are some things to help you out :+)

It's a good idea to declare, initialise and optionally comment each variable, one per line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std; // avoid doing this, do std::cout etc instead, Google as to why this is

int main(){
	double managersalary, cooksfixedrate, salespersonsalary, commissionrate, cookstotalworkinghours, salespersontotalsales, totalincome;
	, manager, cooks, salesperson; 

	const double managersalary = 1000.00;
	const double cooksfixedrate = 15.00;
	const double salespersonsalary = 300.00;
	const double commissionrate = 0.085;

        double cookstotalworkinghours = 0.0;
        double salespersontotalsales = 0.0;
        double totalincome = 0.0;

        const double cooksnormalhours = 30.0;  // normal number of hours per week
        const double overtimefactor = 1.5;  // Time and a half rate for overtime
	position = manager, cooks, salesperson;


I added extra variables so you don't have magic numbers like 30.0 and 1.5 littered throughout the code, use these variable names in your code instead of the numbers.

Line 17, 22, 36:

The = operator is for assignment, you need the == operator which is for equality comparison.

With the if else if statements, rather than put the code for each action in the statement itself, consider having each if or else if call a function. This will tidy up the code considerably, and is good practise.

You don't need variables for each of the roles cook etc, just use the position variable.

Another way to achieve the same thing is to use a switch statement, with each case: calling a function. First, call a void function which shows a menu of options. Provide a quit option as well as an option for each of the roles cook etc. With the switch, provide a default: case to catch bad input, and put the whole thing into a bool controlled while loop:

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
ShowMenu(); // put a declaration for this before main, and provide a definition for it after main.
                    // same for other functions below

//get user input
char position = 'z' ;   // can be 'm' , 'c' , or 's' for each role
std::cin >> position;

bool quit = false;

while (!quit) {

     switch (position) {
          case 'm' :
          case 'M' :  // allow for upper and lower case input
                 // call function to process manager payment
                 break;

          case 's' :
          case 'S' :  // allow for upper and lower case input
                 // call function to process Salesperson payment
                 break;

         case 'c' :
         case 'C' :  // allow for upper and lower case input
                 // call function to process Cook payment
                 break;

         case 'q' :
         case 'Q' :  // allow for upper and lower case input
                 // user wants to quit
                 quit = true;
                 break;
         default:
                 // print message about bad input
                 break;
     }  // end switch

} //end while 


On line 32 there is a problem with your parentheses not being in the right places.

With the number of hours worked, there should be some validation of the amount entered. Negative values are obviously wrong, and there is probably some sort of maximum as well.

Good Luck - we all look forward to seeing your new code <8+D
Topic archived. No new replies allowed.