using loops with structs

Apr 1, 2016 at 5:05am
hello, I am calculating total pay for information stored in structs. total pay is calculated by an adding 10% of base pay for every five (5) hours over forty (40) to base pay. (ie a
person working 50 hours with a total pay of $100 would have ((hours-40)/5)*(base pay*.1) + base pay.

When i run the program I get an undefined number as the output. Am i not using the correct loops to do this calculation?

below is the function i have to calculate the total pay.

1
2
3
4
5
6
7
8
9
10
11
  void Calc_jd(Payroll a[], int size)
{
		for(int index= 0 ; index < num_workers;index ++)
		{
			a[index].total_pay = a[index].hours_worked * a[index].pay_rate;

			if(a[index].hours_worked > 40){
				((a[index].hours_worked-40)/5)*(a[index].total_pay*.1) + a[index].total_pay;
			}
		}
}
Apr 1, 2016 at 6:36am
Shouldn't you be using size instead of num_workers?
Apr 1, 2016 at 8:43am
The Calc_jd does not look like a member function and num_workers is not a function local name. If that compiles, then you have global variables. Globals are usually a rather restrictive design.


Should the line 8 store its result to somewhere? (That does not cause the observed error.)
Apr 1, 2016 at 3:59pm
yes line 8 should store result to the element array of the total pay only if hours work is greater than 40. Here is the full program that i have completed so far.
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
#include <iostream>
#include <iomanip>
using namespace std;
const int num_workers = 10;
struct Payroll
{
	int employee_id;
	double hours_worked;
	double pay_rate;
	double total_pay;


};

void get_jd(Payroll[], int);
void Calc_jd(Payroll[], int);
void get_jd(Payroll a[], int size)
{
	for(int index= 0 ; index < size;index ++)
	{
      cin >> a[index].employee_id;
	}
	cout << endl;
	cout << "enter pay rate for employee#" << endl;
	for(int index= 0 ; index < size;index ++)
	{
		cout << a[index].employee_id <<": ";
		cin >> a[index].pay_rate;
	}
	cout << endl;
	cout << "enter hours worked for employee#" << endl;
	for(int index= 0 ; index < size;index ++)
		{
			cout << a[index].employee_id <<": ";
			cin >> a[index].hours_worked;
		}
}
void Calc_jd(Payroll a[], int size)
{
		for(int index= 0 ; index < size;index ++)
		{
			a[index].total_pay += a[index].hours_worked * a[index].pay_rate;

			if(a[index].hours_worked > 40){
				((a[index].hours_worked-40)/5)*(a[index].total_pay*.1) += a[index].total_pay;
			}
		}
}
int main() {
	Payroll admin[num_workers];
	Payroll office[num_workers];
	Payroll field[num_workers];
	cout << " enter employee id numbers for 10 employee" << endl;

	get_jd( admin,num_workers);
	cout << admin[1].total_pay << endl;
	return 0;
}

im getting a error with my if statment. not sure if I am following the right logic on this
Last edited on Apr 1, 2016 at 4:00pm
Apr 1, 2016 at 4:14pm
Thanks for not telling us what the error is. This is so much more fun when we have to do more of the work.

[/sarcasm]

EDIT: I don't see anything wrong with your if statement, but I do see something wrong with the line following it, line 45. You're attempting to modify the value of ((a[index].hours_worked-40)/5)*(a[index].total_pay*.1) But that makes no sense, because ((a[index].hours_worked-40)/5)*(a[index].total_pay*.1) isn't a variable you can just set the value of.

In technical language, we call ((a[index].hours_worked-40)/5)*(a[index].total_pay*.1) an rvalue, because it can only be used on the right-hand side of an assignment expression.

What is it that you're actually trying to change the value of?
Last edited on Apr 1, 2016 at 4:19pm
Apr 1, 2016 at 4:34pm
When hours worked is greater than 40 I would like to change the value of the elements in total pay that make the condition true. Would a do while loop be more efficient for this?
Apr 1, 2016 at 5:00pm
To paraphrase what MikeyBoy said regarding line 45, you can't do calculation on the left side of an assignment statement. The calculation belongs on the right hand side. I suspect you simply got the statement backwards.
 
  a[index].total_pay += ((a[index].hours_worked-40)/5)*(a[index].total_pay*.1);


Would a do while loop be more efficient for this?

No. All loops generate pretty much the same code. Use whatever form of a loop suits the problem. i.e. Use a for loop when you want to iterate over a known number of objects. Use a while or do/while to loop while a condition is true. Worrying about the efficiency of one type of loop over another is generally a waste of time.

Apr 1, 2016 at 5:03pm
ok i made the correction by putting a[index].total_pay = (a[index].hours_worked-40)/5)*(a[index].total_pay*.1
but i still end with the output of 4.94066e-324.

1
2
3
4
5
6

if(a[index].hours_worked > 40){
			a[index].total_pay =	((a[index].hours_worked-40)/5)*(a[index].total_pay*.1) + a[index].total_pay;
			}
		}
}
Apr 1, 2016 at 5:28pm
Yes. What happens between lines 55 and 56?
Topic archived. No new replies allowed.