Array & Function

A bookstore owner wants you to write a program to calculate the store's weekly payroll. The store's employees are paid hourly. Overtime is to be paid at the rate of 1.5 times the normal rate for hours an
employee worked over 40.

The program should prompt the user to enter the number of hours the employee worked and the payrate for the employee. The program should display the regular pay, overtime pay, and gross pay for that employee. Then the program should prompt the user to enter the data for another employee. When theuser responds that there are no more employees to process, the program should display the number of employees it processed and the total payroll, that is, the sum of the gross salaries of the employees

The program should validate pay rate as it is entered. The only valid hourly pay rates are the following:

5.50, 6.00, 6.50, 6.75, 7.00, 7.25, 7.50, 7.75, and 8.00.

Store the rates in an array. When a pay rate is entered, search the array for a match. If there is a match, continue the rest of the calculations. If there is
no match, display an error message, a list of the valid pay rates, and prompt for another pay rate.

Here is my code. somehow it works. But I not sure whether my calculations are right or not and I want to find a way more efficient way to write this program.
Advice are much appreciated. :D

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
#include<iostream>
#include<iomanip>
#include<cmath>
double calc_pay(int hour,double payrate);
using namespace std;

int main()
{
	int k=0,hour;
	double A_payrate[]={5.50, 6.00, 6.50, 6.75, 7.00, 7.25, 7.50, 7.75, 8.00};
	char choice='y';
	double grosspay=0,payrate,total_payroll=0;
	while(1)
	{
		cout<<"For employee #"<<k+1<<": "<<endl;
	    cout<<"Enter the number of working hours: ";
	    cin>>hour;
	do
	{
		cout<<"Enter the Hourly pay rate: Choose from below: \n"<<endl;
		cout<<"( 5.50, 6.00, 6.50, 6.75, 7.00, 7.25, 7.50, 7.75, 8.00 ): ";
		cin>>payrate;
		if(payrate==5.50 || payrate==6.00 || payrate==6.50 || payrate==6.75 || payrate==7.00 || payrate==7.25 || 
			payrate==7.50 || payrate==7.75 || payrate==8.00)
		{
			cout<<"You have selected "<<setprecision(2)<<fixed<<payrate<<" as payrate "<<endl;
		}
		if(!(payrate==5.50 || payrate==6.00 || payrate==6.50 || payrate==6.75 || payrate==7.00 || payrate==7.25 || 
			payrate==7.50 || payrate==7.75 || payrate==8.00))
		{
			cout<<"Invalid Payrate. Please reenter again : ";
		    cin>>payrate;
			cout<<"You have selected "<<setprecision(2)<<fixed<<payrate<<" as payrate "<<endl;
		}
	} while(payrate==5.50 && payrate==6.00 && payrate==6.50 && payrate==6.75 && payrate==7.00 && payrate==7.25 &&
			payrate==7.50 && payrate==7.75 && payrate==8.00);

      grosspay = calc_pay(hour,payrate);
      k++;
      total_payroll += grosspay;

     cout<<"\nContinue? (y/n) : ";
     cin>>choice; cout<<endl;
     if(choice != 'y')
     {
      cout<<"You have selected n to continue."<<endl;
      break;
     }
   }
    cout<<fixed<<setprecision(2);
    cout<<"The total No of Employee is "<<k<<endl;
    cout<<"The total payroll "<<total_payroll<<endl;

	system("Pause");
    return 0;
}
double calc_pay(int hour,double payrate)
{
  if(hour>40)
    {
    return 40 * payrate + (hour-40) * 1.5 * payrate;
    }
  else
   {
   return hour*payrate;
   }
}
Last edited on
you may use bool variable for loop
like
1
2
3
4
5
6
7
8
9
10
11
bool isValid=false;

do
{
   isValid=false;
   for(int i=i;i<9;i++)
  {
     if(value==a_payrate[i])
      isValid=true;
   }
}while(!isValid);

c++ assignment help
http://homeworkhelp4u.com/cpp_programming_homework_help.html
Last edited on
Three points:

1. https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

2. You have:
1
2
cout << "Enter the Hourly pay rate: Choose from below: \n" << endl;
cout << "( 5.50, 6.00, 6.50, 6.75, 7.00, 7.25, 7.50, 7.75, 8.00 ): ";

But there the values are fixed and do not use the A_payrate array (nothing in your program uses that array. Try this:
1
2
3
cout << "Enter the Hourly pay rate: Choose from below: \n\n(";
for ( auto pr : A_payrate ) cout << ' ' << pr;
cout << " ): ";


3. Your indentation is not systematic. It is hard to notice that the while on line 13 ends at line 49.
@codehelper thanks for the reply. Can I ask which is the loop at which line that you are stating? :)

@keskiverto thanks for the reply. For the second point, the auto pr :
what is this about ? can you tell me more ? :)
But I not sure whether my calculations are right or not

Why don't you test it?
Get a calculator and create some test data. Run the program enter the data and see if the results are what you expected.

There are ways to improve your code.
1. Use more functions
2. Use constants
3. Use descriptive variable names numEmployess instead of k

For example:
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
const double PAY_RATES [] = { 5.50, 6.00, 6.50, 6.75, 7.00, 7.25, 7.50, 7.75, 8.00 };
const int NUM_RATES = sizeof(PAY_RATES) / sizeof(PAY_RATES[0]);

bool equal(float a, float b) 
{
  return fabs(a - b) < numeric_limits<float>::epsilon();
}

bool validRate(float rate)
{
  for (float f : PAY_RATES)
    if (equal(f, rate))
      return true;

  return false;
}

bool confirm(const string& msg)
{
  cout << msg;
  cin.clear();
  cin.ignore(255, '\n');
  char ch = cin.get();

  return ch == 'y' || ch == 'Y';
}
Thank you guys. I take my time and revise it :)
Topic archived. No new replies allowed.