Exercise

This is just an exercise.
And i need to complete this.

This is the question :

Write a program in C++ that calculates the amount to be paid to an employee based on the hours worked and rate per hour. A user will enter hours worked and rate per hour for an employee. Your program should have a function payCheck that calculates and returns the amount to be paid. The formula for calculating the salary amount is as follows: for the first 40 hours, the rate is the given rate (the rate that a user has entered); for hours over 40, the rate is 1.5 times the given rate.


i had write my own coding, but it keeps error. it is okay. But i still don't think i could share here?
Still running, but anyone can help me?
i will update my coding for you alls to check for any syntax or logic error.

UPDATE : this is my coding.
The problem is i need to calculate if the employee work for more than 40 hours which 1.5 times.

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
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>

using namespace std;


int main(void)
{
	char *empname, *test, *final_output;
	float hours=0, pay_rate=0, gross=0, net=0;

//loop
	do {
		/*-- get first & last name --*/
		cout<<"Please Enter Your Name ";
		cin>>empname;

		/*-- 
			get pay rate with error checking on
			null and 0 values
		--*/
		do {
			cout << "Pay Rate: ";
			cin >> test;
			if (test == NULL) 
				cout << "Invalid pay rate.";
			else if(atof(test) != 0) 
				break;
			else
				cout << "Invalid pay rate.";			
		} while (1);
		pay_rate=atof(test);
			
		/*--
			get hours worked with error checking on
			null and 0 values
		--*/
		do {
			cout << "Hours Worked: ";
			cin >> test;
			if (test == NULL)
				cout << "Invalid amount of hours.";
			else if(atof(test) !=  0)
				break;
			else
				cout << "Invalid amount of hours.";
		} while (1);
		hours=atof(test);

		/*-- calculate values --*/		
		gross=hours*pay_rate;

		/*-- set out precision --*/
		cout.precision(2);
		cout.setf(ios::fixed | ios::showpoint);
		
		/*-- create output --*/
		sprintf(final_output,"\n\n%s %s ssn(%s)\n------------------------------\n",
				empname);
		cout << final_output;
		cout << "Gross: " << gross << endl;

		/*-- do another? --*/
		cout << "Calculate Another? (y/n)?";
		cin >> test;

		/*-- check first value of string for y/Y --*/
	} while (!strncasecmp(test,"y",1));

	return 0;
}
Last edited on
closed account (o3hC5Di1)
Hi there,

You state you are getting errors from your code. Please include both your code and the errors, so we have an idea of what the problem might be.

All the best,
NwN
Of course i will.

i am still running. Soon.

Anyway, thanks for the reply.
This is my latest code
It does not have an error, but i cannot calculate the rate of overtime 4O hours.
i am still running my exercise and need help.

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
//Write a program in C++ that calculates the amount to be paid
//to an employee based on the hours worked and rate per hour.
//A user will enter hours worked and rate per hour for an employee.
//Your program should have a function payCheck that calculates and
//returns the amount to be paid. The formula for calculating
//the salary amount is as follows: for the first 40 hours,
//the rate is the given rate (the rate that a user has entered);
//for hours over 40, the rate is 1.5 times the given rate.

#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>

using namespace std;

int main(void)
    {
    char *empname, *test, *final_output;
    float hours=0, pay_rate=0, emprate=0;
    float pay_rate_overtime=1.5;

//variable
    final_output = new char[1024];
    empname = new char[50];
    test = new char[10];

//loop
    do {
        cout << "\nPlease Enter Your Name : ";
        cin >> empname;
        
    do {
        cout << "\nEnter Your Pay Rate : ";
        cin >> test;
        if (test == NULL)
        cout << "Invalid pay rate.";
        else if(atof(test) != 0)
        break;
        else
        cout << "please enter digit only";            
        }
    while (1);
        pay_rate=atof(test);
    
    do {
        cout << "\nEnter Your Worked Hours: ";
        cin >> test;
        if (test == NULL)
        cout << "please enter digit only";
        else if(atof(test) !=  0)
        break;
        else
        cout << "Invalid amount of hours.";
        }
    while (1);
        hours=atof(test);

        /*-- calculate values --*/        
        emprate=hours*pay_rate;

        /*-- set out precision --*/
        cout.precision(2);
        cout.setf(ios::fixed | ios::showpoint);
        
        /*-- create output --*/
        sprintf(final_output,"\n\n Your Pay Rate is : \n------------------------------\n", empname);
        cout << final_output;
        cout << "Your Salary Is : " << emprate <<endl;

        /*-- do another? --*/
        cout << "\n\n Calculate Another? (y/n)?";
        cin >> test;

        /*-- check first value of string for y/Y --*/
    } while (!strncasecmp(test,"y",1));

    /*-- return memory to the heap --*/
    delete [] empname;
    delete [] test;
    return 0;
}
Rather than simply doing this:
 
    emprate=hours*pay_rate;

you should first be checking whether hours is greater than 40.
If so, would then split this into
normal_hours = 40
overtime_hours = hours - normal_hours

or something like that.
This is my latest code
It does not have an error, but i cannot calculate the rate of overtime 4O hours.
i am still running my exercise and need help.

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
//Write a program in C++ that calculates the amount to be paid
//to an employee based on the hours worked and rate per hour.
//A user will enter hours worked and rate per hour for an employee.
//Your program should have a function payCheck that calculates and
//returns the amount to be paid. The formula for calculating
//the salary amount is as follows: for the first 40 hours,
//the rate is the given rate (the rate that a user has entered);
//for hours over 40, the rate is 1.5 times the given rate.

#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>

using namespace std;

int main(void)
    {
    char *empname, *test, *final_output;
    float hours=0, pay_rate=0, emprate=0;
    float pay_rate_overtime=1.5;

//variable
    final_output = new char[1024];
    empname = new char[50];
    test = new char[10];

//loop
    do {
        cout << "\nPlease Enter Your Name : ";
        cin >> empname;
        
    do {
        cout << "\nEnter Your Pay Rate : ";
        cin >> test;
        if (test == NULL)
        cout << "Invalid pay rate.";
        else if(atof(test) != 0)
        break;
        else
        cout << "please enter digit only";            
        }
    while (1);
        pay_rate=atof(test);
    
    do {
        cout << "\nEnter Your Worked Hours: ";
        cin >> test;
        if (test == NULL)
        cout << "please enter digit only";
        else if(atof(test) !=  0)
        break;
        else
        cout << "Invalid amount of hours.";
        }
    while (1);
        hours=atof(test);

        /*-- calculate values --*/        
        emprate=hours*pay_rate;

        /*-- set out precision --*/
        cout.precision(2);
        cout.setf(ios::fixed | ios::showpoint);
        
        /*-- create output --*/
        sprintf(final_output,"\n\n Your Pay Rate is : \n------------------------------\n", empname);
        cout << final_output;
        cout << "Your Salary Is : " << emprate <<endl;

        /*-- do another? --*/
        cout << "\n\n Calculate Another? (y/n)?";
        cin >> test;

        /*-- check first value of string for y/Y --*/
    } while (!strncasecmp(test,"y",1));

    /*-- return memory to the heap --*/
    delete [] empname;
    delete [] test;
    return 0;
}
closed account (o3hC5Di1)
Hi there,

Why did you copy your earlier post? Chervil kindly offered you a solution in his reply, yet I don't see that in your code. If you require further help, please state your problem clearly and specifically.

All the best,
NwN
Topic archived. No new replies allowed.