Payroll Summary

How would I be able to make recently processed data to:

1. Display a table at the end of the program listing the name and gross pay of each employee processed.

2. Use the data stored in the arrays to recalculate and display the total gross pay of the employees entered.


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
  int main()
{
    std::string first_name,             // employee's first name
                last_name,              // employee's last name
                full_name;              // employee's concatenated full name

    double      hours,                  // number of hours worked
                regular_hours,          // number of regular work hours
                overtime_hours,         // number of overtime work hours
                hourly_rate,            // hourly pay rate
                gross_pay,              // employee's gross pay
                net_pay,                // employee's net pay
                tax,                    // employee's tax amount
                total_gross_pay,        // accumulated gross pay
                deductions;             // monthly pay deductions
                

    int         emp_count;              // employee count being processed   

    char        answer;                 // user's yes or no response

    std::cout << std::fixed << std::setprecision(2);

    total_gross_pay = 0;
    emp_count = 0;

    // WELCOME MESSAGE //
    welcome_message();
    
    do { 
        // INPUT SECTION //
        input_employee_data (full_name, hours, hourly_rate, deductions);

        // PROCESSING SECTION //
        split_hours (hours, regular_hours, overtime_hours);
        gross_pay = calculate_gross_pay (regular_hours, overtime_hours, hourly_rate); 
        tax = calculate_tax (gross_pay, TAX_RATE);      
        net_pay = calculate_net_pay (gross_pay, tax, deductions);
  
        // OUTPUT SECTION //
        output_payroll_data (full_name, regular_hours, overtime_hours, hourly_rate, gross_pay, 
                             tax, deductions, net_pay);

        // PROCESS ANOTHER EMPLOYEE ?
        get_yesno ("Process another employee", answer);
        std::cout << std::endl;

        // COUNTER AND ACCUMULATOR
        total_gross_pay = total_gross_pay + gross_pay;
        emp_count++;
        

    } while (answer == 'Y'); 

    // Summary of the whole Process to go below... 
Last edited on
Hello ashley50,

Based on what I see here and what I do see, the other functions, All you could do is put something at the end of the do/while loop not after.

I would consider creating a struct or class to hold the data for each employee and then use a vector or array to store each struct for later use. That way after the do/while loop you can step through vector or array to process the data and add some parts to a total for final output.

I see in main you define "first" and "last" name along with "full_name", but all I can see is that you only use "full_name", so I do not know if you split the into "first" and "last" names.

I see where you call the functions:
1
2
3
4
split_hours (hours, regular_hours, overtime_hours);
gross_pay = calculate_gross_pay (regular_hours, overtime_hours, hourly_rate); 
tax = calculate_tax (gross_pay, TAX_RATE);      
net_pay = calculate_net_pay (gross_pay, tax, deductions);

In line 4 you use the variable "deductions", but where did you figure "deductions"?

Looking at your function calls I am guessing that you are passing most of the variables by reference which will work, but I am not sure with out seeing the proto types of functions.

You have a very good start, but for what you want there needs some rework to do what you want.

Hope that helps,

Andy
I didn't include the functions as it would go over the length limit.

This is function for full name, which already concatenates their name to "last_name, first_name".
1
2
3
4
5
6
7
8
std::string join_name(std::string first_name, std::string last_name)
{   
    std::string full_name;

    full_name = last_name + ", " + first_name;

    return full_name;
}


The Deductions Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double get_deductions (std::string question)
{   
    char    answer;

    double  deductions;

    do {
        get_yesno ("Does the employee use the Parking Garage?", answer);
        if (answer == 'Y') {
            deductions = PARKING_RATE;
        } else if (answer == 'N') {
            get_yesno ("Does the employee participate in the transit program", answer);
            deductions = TRANSIT_RATE;
        } 
        if (answer == 'N') {
            deductions = ZERO_DEDUCTION;
        }
    } while (answer != 'Y' && answer != 'N');

    return  deductions;
}


The rest of the functions are coded properly

I'm still new to arrays and still figuring out on how to properly output the desired results I would want.

We were given the solution by our instructor so we could study how arrays behave and how to produce the results that was asked of us.
Hello ashley50,

Lines 7 - 15 along with 18 and 20 it is a good practice to always initialize your variables. The simplest way is to follow the variable name with empty {}s. You can also put a number inside the {}s if needed.

You are making it difficult for me to help you. If your program is that big try GitHub https://github.com/ for posting your code. I have seen this used many times along with some other sites.

You talk about using arrays, but I see nothing in your code that uses any arrays. My first thought is to create a struct to hold the information and then put the struct into an array for later use.

I am thinking something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Info
{
	std::string first_name,         // employee's first name
		last_name,              // employee's last name
		full_name;              // employee's concatenated full name

	double  hours{},                  // number of hours worked
		regular_hours{},          // number of regular work hours
		overtime_hours{},         // number of overtime work hours
		hourly_rate{},            // hourly pay rate
		gross_pay{},              // employee's gross pay
		net_pay{},                // employee's net pay
		tax{},                    // employee's tax amount
		total_gross_pay{},        // accumulated gross pay
		deductions{};             // monthly pay deductions
};


With the information in a struct stored in an array you have the ability to process the information any way you like later in the program.

I will probably think of more after I post this.

Any questions just ask.

Hope that helps,

Andy
I've never used github before until now.

uploaded the current cpp file I'm working on.

https://github.com/ashley51/payroll_prog/blob/master/Lab12b_Payroll_w_Functions_COPY.cpp

I am currently changing names/values on the code below, running it and picturing how I could use it to achieve the task given.

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#define SIZE 10       // added Constant

int input_number (int min_value, int max_value);
char get_yesno (std::string question);
int compute_sum(int numbers[], int size);

int main()
{
    int gross[SIZE],  //  array of numbers entered by the user   
        count,          //  count of numbers entered
        sum,            //  sum of numbers entered
        i;

    // Read numbers from the user
    count = 0;
    sum = 0;
    do {
        gross[count] = input_number(0, 5000);
        count = count + 1;          // or count++; or count += 1;
        
        // COMPUTE SUM HERE
        sum = compute_sum(gross, count);

    } while ((count < SIZE) && (get_yesno("Enter another number") == 'Y')); 
    
    for (i = 0; i < count ; i++) {
        std::cout << i << " is " << gross[i] << std::endl;
    }



    // Display the count and sum
    std::cout << std::endl;
    std::cout << "You entered " << count << " numbers" << std::endl;
    std::cout << "Their sum is " << sum << std::endl;

    system("pause");
    return 0;
}

//
// An input module that reads an integer from the user, bounded between
// the given minimum and maximum values.
//
int input_number(int min_value, int max_value)
{
    int gross;  // number read from the user

    do {
        std::cout << std::endl;
        std::cout << "Enter a number from " << min_value << " to "
                  << max_value << ": ";
        std::cin >> gross;
        if (gross < min_value || gross > max_value) {
            std::cout << "The number " << gross << " is not in range!"
                      << std::endl;
        }
    } while (gross < min_value || gross > max_value);

    return gross;
}

//
// An input module that gets a validated yes or no from the player, in
// response to a given question. The player's response is normalized to
// an upper-case 'Y' or 'N' only.
//
char get_yesno(std::string question)
{
    char answer;  // player's yes/no response

    do {
        std::cout << std::endl;
        std::cout << question << " (Y/N)? ";
        std::cin >> answer;

        // Convert lower-case responses to upper case
        if (answer == 'y') {
            answer = 'Y';
        } else if (answer == 'n') {
            answer = 'N';
        }

        if (answer != 'Y' && answer != 'N') {
            std::cout << "Please enter 'Y' for yes or 'N' for no"
                      << std::endl;
        }
    } while (answer != 'Y' && answer != 'N');

    return answer;
}

// Totals the values in a given array.  The number of elements in the array
// is indicated by the 'size'

int compute_sum(int gross[/* leave this space empty */], int size)
{
    int     i,      // loop control variable
            sum;    // sum of numbers in array

    sum = 0;
    for (i = 0 ; i < size ; i++) {
        sum += gross[i];
    }
    return sum;
}


Last edited on
Hello ashley50,

Thank you the link to GitHub it worked. Now many parts of the program make more sense.

Just some FYI for your consideration.

As an example.
1
2
3
    for (i = 0 ; i < size ; i++) {
        sum += gross[i];
    }


Although there is nothing wrong with writing the {}s like this, it works. But when you write a block of code that could span more than one screen it makes it hard to see where the opening and closing {}s are.

I prefer this because it makes it easier to find the opening and closing {}s.

1
2
3
4
    for (i = 0 ; i < size ; i++)
    {
        sum += gross[i];
    }


Not only is it easier to see the opening and closing{}s. If the closing brace is in the next screen with the opening and closing {}s being in the same column it is easier to match up. Also should you have 3 or 4 lines of closing {}s it is easier to find when you are missing a closing }.

Not real important just something to think about.

The "get_yesno" function could be done in an easier way and still have th ame result along with shorting the code.

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
char get_yesno(std::string question)
{
	char answer;  // player's yes/no response

	do {
		std::cout << std::endl;
		std::cout << question << " (Y/N)? ";
		std::cin >> answer;

		answer = toupper(answer);

		// Convert lower-case responses to upper case
		//if (answer == 'y') {
		//	answer = 'Y';
		//}
		//else if (answer == 'n') {
		//	answer = 'N';
		//}

		if (answer != 'Y' && answer != 'N') {
			std::cout << "Please enter 'Y' for yes or 'N' for no"
				<< std::endl;
		}
	} while (answer != 'Y' && answer != 'N');

	return answer;
}


It is possible that you could pass "answer" by reference and not need the return value.

If you are familiar with structs I refer you to my earlier post about the struct. If not I will help you all I can to understand.

The in main you would only have to define one array like: Info aEmp[MAXSIZE];. The "a" gives you a hint that the variable is an array. Not really necessary if you do not want to use it. I used "MAXSIZE" over the "SIZE" as I see in your previous post. Either one will work, but "MAXSIZE" is more often seen used here. in the end it is your choice.

As I started to work with what you have in your OP I found the struct and array of structs to be the easiest to use. This way you only have to pass one array to functions along with the count of how many elements that you used.

To work without using the struct you would have to create several arrays to hold all the information that you will need. And then passing all those arrays to functions would be tedious.

While I am thinking about it the "join_name" function is not really needed. This can just as easily be done right after you enter the "first" and "last" names with out the need to call a function. Since it is there you can still use it.

Now that I can see the whole program I will need a little time to go through it and see what needs to be changed.

Hope that helps,

Andy
Thanks for suggesting the 'toupper' and a better way to place the '{ }' brackets.

The bracket placement you suggested makes sense that it does make it easier to spot and match any open brackets in that format.

Thank you.
You are welcome. Any time.

Andy
Topic archived. No new replies allowed.