payroll program question

Hello,
I am trying to get my loop working but it's not printing anything out after fica withholdings.

Description on program :

Write a program that displays a weekly payroll report. A loop in the program should ask the user for the employee number, gross pay, state tax, federal tax, and FICA withholdings.
The loop will terminate when 0 is entered for the employee number. After the
data is entered, the program should display totals for gross pay, state tax, federal tax, FICA withholdings, and net pay.
Input Validation: Do not accept negative numbers for any of the items entered.
Do not accept values for state, federal, or FICA withholdings that are greater than the gross pay. If the sum state tax + federal tax + FICA withholdings for any employee is greater than gross pay, print an error message and ask the user to re-enter the data for that employee.

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
 

#include <iostream>


using namespace std;

int main()
{
	int employee_number = 1;
	bool error;
	double gross_pay = 0, total_gross_pay = 0, total_withholdings, net_pay;
	double Federal_withholding = 0, total_Federal_withholding = 0;
	double State_withholding = 0, total_State_withholding = 0;
	double Fica_withholding = 0, total_Fica_withholding = 0;
	total_withholdings = Federal_withholding + State_withholding + Fica_withholding;
	net_pay == gross_pay - total_withholdings;
	while (employee_number != 0)
	{
		cout << "Employee Number (0 to Quit):";
		cin >> employee_number;
		while (employee_number < 0)
		{

			cout << "employee number may not be less than zero" << endl;
			cout << "Re-enter employee number";
			cin >> employee_number;


		}
		if (employee_number != 0)
		{
			do
			{
				cout << "Gross pay: ";
				cin >> gross_pay;


				while (gross_pay < 0)
				{
					cout << "Gross pay may not be less than zero" << endl;
					cout << "Re-enter Gross pay";
					cin >> gross_pay;
					error = true;
				}



				cout << "Federal Witholding: ";
				cin >> Federal_withholding;


				while (Federal_withholding < 0)
				{
					cout << "Federal Withholding may not be less than zero" << endl;
					cout << "Re-enter Federal Withholding";
					cin >> Federal_withholding;
					error = true;
				}



				cout << "State Witholding: ";
				cin >> State_withholding;


				while (State_withholding < 0)
				{
					cout << "State Withholding may not be less than zero" << endl;
					cout << "Re-enter State Withholding";
					cin >> State_withholding;
					error = true;
				}



				cout << "Fica Witholding: ";
				cin >> Fica_withholding;


				while (Fica_withholding < 0)
				{
					cout << "Fica Withholding may not be less than zero" << endl;
					cout << "Re-enter Fica Withholding";
					cin >> Fica_withholding;
					error = true;
				}
				if (total_withholdings > gross_pay)
				{
					cout << "Your withholdings cant be greater than your gross pay. Please re-enter" << endl;
					error = true;
				}
				else
					error = false;

				







			} while (error);
			total_gross_pay += gross_pay;
			total_State_withholding += State_withholding;
			total_Federal_withholding += Federal_withholding;
			total_Fica_withholding += Fica_withholding;





		}

	}
	cout << "Gross Pay Total is $" << total_gross_pay << endl;
	cout << "State Tax Total is $" << total_State_withholding << endl;
	cout << "Federal Tax Total is $" << total_Federal_withholding << endl;
	cout << "FICA Total is $" << total_Federal_withholding << endl;

	return 0;

































}


Last edited on
Hello vladdy,

Welcome to the forum.

I have to ask out of all the loops that you have which one is the problem. With out testing I do not see anything that is a problem.

For line 31 I would change the "!=" to ">" just to make sure the variable is above zero. And may be an else section(optional).

There are a few to many blank lines. To the compiler this means nothing. to the reader it looks like there might be something missing and it makes it a bit confusing. There are one ot two place that could use a blank line and do not have one.

I will load the program up in the morning and see what happens.

Hope that helps,

Andy
Hello vladdy,

After testing the program I found it useful to add the header file "<iomanip>" more later.

Try to avoid using using namespace std; in your programs it may seem easy now, but WILL get you in trouble some day.

It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy.

What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.

In line 12 I would suggest initializing the variables "total_withholdings" and "net_pay" here. The use of empty{}s will do the job, example, net_pay{};. Should you need it you can put a number between the {}s.

Lines 16 and 17 are not really needed.

The outer while loop at line 18 appears to work so far. I have not found any way to break it, but i am still trying.

In line 20 I added a "\n" to the line, as with other places, to make what is displayed on the screen not run together. std::cout << "\nEmployee Number (0 to Quit):";My personal preference is to put a space between the "\n" and the "E" just to keep it off the edge.

The if statement and the do/while works, but I do question the need for "error = true;" at the end of the while loops. You can not exit the while loop until a valid entry is entered. Even if that entry is zero. So setting the "error" variable to true has no real effect. And there is the last if statement. Should the if statement be false the else state will set error to false. This tends to negate anything you have done earlier.

What I would consider is to take the lines 117 - 120 copy them and paste them at line 96 then remove the "total_" from the variable names followed be asking the user if everything is correct. If it is correct either set "error" to false or "break;" out of the while loop. This gives you a better opportunity to check your entries before moving on the the next entry.

Now back to the header file "<iomanip>". You can put thisstd::cout << std::fixed << std::showpoint << std::setprecision(2); either at line 96 or just before line 117, so that you can display numbers with a decimal point and two place to the right of the decimal.

My thoughts for now. Overall the program looks good.

Something for the future. State withholding, FICA and possibly a couple of others work off a percentage of the gross income. What you can do is at the beginning, above main, is define these variables as constexpr double STATETAX{ 0.055 };. "const" can also be used if "constexpr" does not work for you. This way as a global variable it is at the beginning of the program where there is only one place to change and it will be seen by the whole file. The capital letters will let you know it is a constant vriable that can not be changed.

Hope that helps,

Andy
Topic archived. No new replies allowed.