数组未显示正确的输出

时间:2016-11-21 09:37:13

标签: c++ c++11

我正在编写这个计算员工工作小时数的程序。如果员工最终工作时间超过40小时,那么他每经过40小时就会获得额外的时间和一半。我的问题是输出时间。在输出中,它显示销售税,联邦税,总收入和净额;但是如果员工投入的时间不到40小时,那么一切都会正常显示;然而,一旦员工投入,他出于某种原因工作超过40小时,它显示税,净,毛为0.00。

例如,如果小于40,将显示以下内容。

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

员工最后39.00 22.00 1.50 60.06 128.70 34.32 858.00

如果超过40,则显示以下内容。

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

员工最后45.00 22.00 1.50 0.00 0.00 0.00 0.00

任何提示,提示,建议都表示赞赏.`

#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 ";
}
}

1 个答案:

答案 0 :(得分:0)

因为在您的情况下,您检查if(hoursWorked[i] < 40)if(hoursWorked[i] > 40),但if(hoursWorked[i] == 40)没有条件。您可能希望在其中一个条件(>=<=)上使用-or-equal运算符。