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>

Recommended Answers

All 7 Replies

All of your variables are declared as floats. Check your data types. Not every piece of data in a program is a floating-point number...

You may also want to look at how you are using GrossPay.

FedTax = GrossPay * 0.20;
StTax = GrossPay * 0.05;
GrossPay = RegularPay + OvertimePay;
NetPay = GrossPay - FedTax - StTax;

These are the first time it's used, but you never put anything valuable in it before you use it.

On a side note, don't use system() or the conio.h header. The conio header is a non-standard header that every compiler implements differently. There are standards-compliant ways of doing much of what you can do with conio.

LNAME is a float....You probably want a string for LNAME.

Don't call system('cls') you're like branching out of your own program to have the system call completed to 'cls' which may or may not exist depending on the system (it will work on windows only as far as I know.) with...

Include <windows.h> and/or <conio.h> if I remember correctly and call clrscr();

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?

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?

Basically yes. Your if statement that checks that is WAAAAAY too late in your code, and it's not in a loop. The previous if (that checks for over 40 hours) caught that situation and responded to it. By the time it got to the if that checks for >60hrs, it was too late. The 60-hour check should have been the first in your if-else if ladder.

variable identifications were wrong. all were created float , you have to classify and reaarange it will work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.