Sentinel Loop

Write a program to process weekly employee time cards for all employees of an organization. Each employee will have three data items: the employee's name, the hourly wage rate, and the number of hours worked during a given week. Employees are to be paid time-and-a-half for all hours worked over 40. A tax amount of 3.625 percent of gross salary will be deducted. The program output should show each employee's name, gross pay, and net pay, and should also display the total net and gross amounts and their averages. Use zzzzzz as a sentinel value for name.

My problem is that ive written a code but i want the program to terminate once the user types in "zzzzzz" but for some reason it goes through the loop one more time and messes up my averages. any help here?
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
using namespace std;
#include <iostream>
#include <iomanip>       
#include <string>

string name;
double wage;
double hours;
double grosspay;
double netpay;
double overtimehours;
double overtimepay;
double normalpay;
double totalnet;
double totalgross;
double averagenet;
double averagegross;
double counter;
string Sentinel = "zzzzzz";

int main()

{
		counter = 0;
		totalnet = 0;
		totalgross = 0;

	while (name != Sentinel)
	{
		totalgross += grosspay;
		totalnet += netpay;
		counter++;
		cout << "Enter employee's name: ";
		cin >> name;
		cout << "Enter hourly wage: ";
		cin >> wage;
		cout << "Enter hours worked this week: ";
		cin >> hours;

		cout << setprecision(2) << fixed;
		if (hours <= 40)
		{ grosspay = wage*hours;
		}
		else
		{ overtimehours = hours-40;
		overtimepay = (overtimehours * wage) * 1.5;
		normalpay = wage*hours;
		grosspay = overtimepay + normalpay;
		}
		cout << endl;
			netpay = grosspay - (grosspay * .03625);
			cout << "Employee name is " << name << endl;
			cout << "Employee's gross pay is $" << grosspay << endl;
			cout << "Employee's net pay is $ " << netpay << endl;
			cout << endl;
	}
		averagegross = totalgross/counter;
		averagenet = totalnet/counter;
		cout << "Total gross pay of employees is " << totalgross << endl;
		cout << "Average gross pay per employee is " << averagegross << endl;
		cout << "Total net pay of employees is " << totalnet << endl;
		cout << "Average net pay per employee is " << averagenet << endl;

		return 0;
}
The program should immediately break out of the loop if the sentinel value is entered, so you should check for it straight after cin >> name and break the loop right there and then. Also, zzzzzz is a terrible sentinel value. If any other number of z letters are entered, then it won't break the loop. Why not a sentinel value of "quit" or something else sensible?
Even if the user has input the sentinel value, the while loop proceeds because we has already entered the block of while loop. The condition of while loop will not be checked at that point.

To make it not go through if the sentinel value is input, you can simply make use of an if statement,
if (name != Sentinel) and then use a block for all code thereafter in the while loop.

Note that you should place the counter increment, counter++ in that if code block.
Otherwise, you will make an off-by-one error.
Last edited on
@Mats sadly, my im in a beginner's c++ class and the instructor has specifically asked us to make zzzzzz the sentinel value. And as to both of your replies, THANK YOU SO MUCH. I couldnt figure that problem out for the life of me and you guys broke it down so simply. i owe you guys.
Oh and just incase someone stumbles upon this thread, this was the code i ended up using

1
2
3
4
5
6
7
8
9
10
11
12
13
...
		while (name != Sentinel)
	{
		totalgross += grosspay;
		totalnet += netpay;
		cout << "Enter employee's name: ";
		cin >> name;
		if (name != Sentinel)
		{
		counter++;

		cout << "Enter hourly wage: ";
...
Last edited on
huh... im confused. for some reason the overtime wage was actually calculated at 2.5* for some reason... not sure what i did wrong here
Topic archived. No new replies allowed.