Instead of
[code]
if (hours_worked > 0 , hours_worked < 40)
[\code]
you need
[code]
if (hours_worked <= 40)
[\code]
because you reach this statement only if hours_worked > 0 (tested in the while loop)
The weird values you get is because you use uninitializsed variables; that means they can have any possible value:
float check;//not initialized; will have a random value
...
if (hours_worked > 40)
{
...
check++;//chech will have the same random value + 1
}
You have to initialise them after you are declaring them:
float check=0, total_payroll=0,...
Awesome!!! Thank you ever so much. Now the only other problem that i am still getting is that when i am using the over time pay -- the figures come out incorrect. According to my assing i need to have something like:
If hours worked was more than 40, then is it 40 tims the hourly rate plus the extra hours over 40 time 1 1/2 times the hourly rate. The statement i used:
gross_pay = (hours_worked * hourly_rate) + ((hours_worked - 40) * (hourly_rate * 1.5));
Seems correct does it not....
But if you try using the code that is now correct by the looks of it:
#include <iostream>
using namespace std;
int main()
{
double hours_worked = 0, hourly_rate = 0, gross_pay = 0, ss_wh = 0, ss_withheld = 0, wh_tax = 0, net_pay = 0, total_payroll = 0, ave_paycheck = 0, total_ss_wh = 0, total_wh = 0;
float checks = 0;
cout << "PAYROLE CALCULATOR\n\n";
cout << "Input number of hours worked (0 to quit):";
cin >> hours_worked;
while (hours_worked > 0)
{
if (hours_worked <= 40)
{
cout << "input hourly rate of pay:";
cin >> hourly_rate;
gross_pay = (hours_worked * hourly_rate);
ss_wh = gross_pay * .08;
wh_tax = gross_pay *.1;
net_pay = gross_pay - ss_wh - wh_tax;
checks++;
total_payroll += gross_pay;
total_ss_wh += ss_wh;
total_wh += wh_tax;
}
if (hours_worked > 40)
{
cout << "Input hourly rate of pay:";
cin >> hourly_rate;
gross_pay = (hours_worked * hourly_rate) + ((hours_worked - 40) * (hourly_rate * 1.5));
ss_wh = gross_pay * .08;
wh_tax = gross_pay *.1;
net_pay = gross_pay - ss_wh - wh_tax;
checks++;
total_payroll += gross_pay;
total_ss_wh += ss_wh;
total_wh += wh_tax;
}
ave_paycheck = total_payroll / checks;
cout << "\nGross pay: " << gross_pay;
cout << "\nSS Withheld: " << ss_wh;
cout << "\nWithholding Tax:" << wh_tax;
cout << "\nNet Pay: " << net_pay;
cout << "\n\nInput hours worked (0 to quit):";
cin >> hours_worked;
}
cout << "\n\nTOTALS";
cout << "\n\nTotal Payroll: " << total_payroll;
cout << "\nNumber of checks: " << checks;
cout << "\nAverage Paycheck: " << ave_paycheck;
cout << "\n\nTotal SS Withheld:" << total_ss_wh;
cout << "\nTotal Witholding: " << total_wh;
cin.get();
cin.get();
return 0;
}
You should get:
PAYROLL CALCULATOR
Enter hours worked (0 to quit):20
Enter pay rate: 10
Gross Pay: 200.00
SS Withheld: 16.00
Withholding Tax: 20.00
Net Pay: 164.00
Enter hours worked (0 to quit): 50
Enter pay rate: 10
Gross Pay: 550.00
SS Withheld: 44.00
Withholding Tax: 55.00
Net Pay: 451.00
Enter hours worked (0 to quit): 0
TOTALS
Total Payroll: 750.00
Number of Checks: 2
Average Paycheck: 375.00
Total SS withheld: 60.00
Total Withholding: 75.00
(bolded to show input values) But when i try doing this...the over time values are off slighty still ---- Do i need to empty a value or reassign that value to zero maybe? The values im getting are:
650
52
65
533
not quite sure what its/im doing wrong. But once again thank you ever so much for your help guys. Your all getting me to learn this stuff faster than either mt TA or my teacher could ever have done(seriously!). Thanks all, hope you can help me figure out this last part ive been struggling with since the beginning. (the wierd numbers i got before were like 1.3eNMAM and stuff, and it was for the totals area, ive always been getting numbers that are a little off for the overtime pay -- so im guessing its the over time equation im using, although ive asked some people and they say it seems correct.
(last thing, these values are dollar values and they need to be with 2 decimal places....eg. 12.00 instead of 12. But when they are printed they do not have decimal places, so i need to caste them or something? -- and if so to what type?)