Sep 21, 2010 at 4:51pm UTC
I'm a beginner (3wks into C++)
Payroll program is due in 1hour.
I can't figure out what is wrong with my program.
it compiles, builds, and runs but when I enter the name, it goes straight to the end of the program.
heres the code
<code>
#include <conio.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float LName, FName, ID, TotalHours, PayRate, RegularHours, OvertimeHours, GrossPay, RegularPay, OvertimePay, FedTax, StTax, NetPay;
cout<<"Enter Last Name: ";
cin>>LName;
system("cls");
cout<<"Enter First Name: ";
cin>>FName;
system("cls");
cout<<"Enter ID #: ";
cin>>ID;
system("cls");
cout<<"Enter Total Hours Worked(Maximum of 60 hours): ";
cin>>TotalHours;
system("cls");
cout<<"Enter Pay Rate: ";
cin>>PayRate;
system("cls");
if( TotalHours < 40 )
{
RegularPay = TotalHours * PayRate;
}
else if( TotalHours >= 40 )
{
RegularHours = 40;
RegularPay = PayRate * 40;
OvertimeHours = TotalHours - 40;
OvertimePay = OvertimeHours * PayRate * 2;
}
if( TotalHours > 60 )
{
cout<<"You Have Entered above Maximum Hours";
}
FedTax = GrossPay * 0.20;
StTax = GrossPay * 0.05;
GrossPay = RegularPay + OvertimePay;
NetPay = GrossPay - FedTax - StTax;
cout<<"\nLast Name: "<<LName<<"First Name: "<<FName<<"ID#: "<<ID;
cout<<"\nTotal Hours: "<<TotalHours<<"Regular Hours: "<<RegularHours<<"Overtime Hours: "<<OvertimeHours<<"Pay Rate: "<<PayRate;
cout<<"\nNetpay"<<GrossPay<<" - "<<FedTax<<"-"<<StTax<<"="<<NetPay;
cout<<"\nHit any key to continue..";
getch();
return 0;
}
</code>
Sep 21, 2010 at 5:16pm UTC
Also, GrossPay is used before it is initialized. Consider reordering these lines:
1 2 3
FedTax = GrossPay * 0.20;
StTax = GrossPay * 0.05;
GrossPay = RegularPay + OvertimePay;
BTW, 1 hour before its due isn't exactly a great time to post an issue. You got lucky.
Last edited on Sep 21, 2010 at 5:17pm UTC
Sep 21, 2010 at 5:30pm UTC
THANK YOU SO MUCH GUYS!!!!!!!
WITH YOUR ADVICES, I JUST FINISHED IT 3 MINS BEFORE DEADLINE!!
Just one more thing..
Howcome my program did not limit the maximum hours of 60?
did I use wrong statement?
Sep 21, 2010 at 5:59pm UTC
You forgot the else and return:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
if ( TotalHours < 40 )
{
RegularPay = TotalHours * PayRate;
}
else if ( TotalHours >= 40 )
{
RegularHours = 40;
RegularPay = PayRate * 40;
OvertimeHours = TotalHours - 40;
OvertimePay = OvertimeHours * PayRate * 2;
}
else if ( TotalHours > 60 )
{
cout<<"You Have Entered above Maximum Hours" ;
return 0;
}
You should have seen the message but it let's you continue as is.
Last edited on Sep 21, 2010 at 6:04pm UTC
Sep 21, 2010 at 7:52pm UTC
The first "else-if" needs to have a test to see if the TotalHours are less than or equal to 60:
else if ( TotalHours >= 40 && TotalHours <= 60 )
otherwise, the code will never reach the last "else-if" statement.
Sep 21, 2010 at 8:22pm UTC
I guess it doesn't even need the else, just the return...