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.
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.
After testing the program I found it useful to add the header file "<iomanip>" more later.
Try to avoid using usingnamespace 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 constexprdouble 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.