ReMaKe ReMaKe - 11 days ago 4
C++ Question

Array not displaying proper output

I'm writing this program that calculates the amount of hours that employees work. If the employee ends up working more than 40 hours, than he'll get an additional time and half for every hour that passes 40. My issue is when it comes to the output. In the output, it shows the amount of sales tax, federal tax, gross income, and net; however if the employee puts in that he has worked less than 40 hours, everything displays properly; however, once the employee puts in that he has worked more than 40 hours for some reason it shows the taxes, and net, gross as 0.00.

For example, if less than 40, the following would display.

F-Name: L-Name: HorsWrked: HrlyRate: TimeHalf: S-Tax: F-Tax: Fica: Gross:

Employee Last 39.00 22.00 1.50 60.06 128.70 34.32 858.00

If more than 40, the following is being displayed.

F-Name: L-Name: HorsWrked: HrlyRate: TimeHalf: S-Tax: F-Tax: Fica: Gross:

Employee Last 45.00 22.00 1.50 0.00 0.00 0.00 0.00

Any tips, hints, advice is appreciated.``

#include <iostream>
#include <iomanip>

using namespace std;

int main()



{
//Set variables
int const size = 2;
double timeHalf = 0.0, hoursWorked[size], gross[size], net[size], hourlyRate[size], totalTax;
string firstName[size], lastName[size];
const double stateTax = 0.07, fedTax = 0.15, fica = 0.04;
double sTax = 0.0, fTax = 0.0, fiTax;




for(int i = 0; i < size; i++) //Initialize for loop
{

cout<<"Please enter your first name? " <<endl; //Display first name
cin>>firstName[i];
cout<<"Please enter your last name? " <<endl; //Display last name
cin>>lastName[i];
cout<<"How many hours did you work? " <<endl; //Dispaly hours worked
cin>>hoursWorked[i];
cout<<"What's the hourly rate? " <<endl; //Display hourly rate
cin>>hourlyRate[i];




if(hoursWorked[i] < 40)
{
gross[i] = hoursWorked[i] * hourlyRate[i];

}


else if( hoursWorked[i] > 40)
{
gross[i] = hoursWorked[i] * (hourlyRate[i] * timeHalf);


}
}

cout<<" \t\tXYX \n ";
cout<<"F-Name: L-Name: HorsWrked: HrlyRate: TimeHalf: S-Tax: F-Tax: Fica: Gross: Net: \n";

for(int i = 0; i < size; i++)
{

sTax = gross[i] * stateTax;
fTax = gross[i] * fedTax;
fiTax = gross[i] * fica;
totalTax = gross[i] * (stateTax + fedTax + fica);
timeHalf = 1.5;
net[i] = gross[i] - totalTax;


cout<<firstName[i]<<"\t " << setw(10) <<fixed <<setprecision(2) << lastName[i]<<"\t "
<< hoursWorked[i]<<"\t " <<hourlyRate[i] <<"\t " <<timeHalf<<"\t " <<sTax <<"\t "
<<fTax <<"\t " <<fiTax <<"\t " <<gross[i] <<"\t " <<net[i] <<"\t ";
}
}

Answer

Because in your conditions, you check if(hoursWorked[i] < 40) and if(hoursWorked[i] > 40), but there is no condition for if(hoursWorked[i] == 40). You probably wanted to use -or-equal operator on one of these conditions (>= or <=).

Comments