使用结构的c ++工资单程序

时间:2014-03-23 17:24:12

标签: c++ struct

我正在尝试使用结构来创建一个程序来存储员工信息。我想包括加班费1.5,轮班工资1.10和定期工资。我希望这能为5名员工循环。程序停在:if (payrollEmp1.hoursWorked > 40)。我得到的错误代码是:

任何帮助都会很棒,谢谢。

#include <iostream>
#include <iomanip>
#include <string.h>
#include <fstream>

using namespace std;

struct data     
{
     //int data; 
     char lastName [25];
     char firstName [25];
     double payRate;
     char shift;
     double monday;
     double tuesday;
     double wednesday;
     double thursday;
     double friday;
     double saturday;
     double sunday;
     double totalHours;
     double overTimePay;
     double income;
     double hoursWorked;
     double regularPay; 
     double shift1;
 };
 //data payrollEmp1[size];

int main ()
{
data payrollEmp1;

const int size = 5;
//data payrollEmp1[size];
// const payrollEmp1.lastName << i+1 << payrollEmp1.firstName;

for (int i=0; i<5; i++)
{
     data payrollEmp1;
     payrollEmp1.payRate = 0;
     payrollEmp1.totalHours = 0;
     payrollEmp1.overTimePay = 0;
     payrollEmp1.income = 0;
     payrollEmp1.hoursWorked = 0;
     payrollEmp1.regularPay = 0;

     cout << "Enter last name: \n";
     cin >> payrollEmp1.lastName;
     cout << "Enter first name: \n";
     cin >> payrollEmp1.firstName;
     cout << "Enter your hourly payrate: \n";
     cin >> payrollEmp1.payRate;
     cout << "Enter your shift; Days (d) or Nights: (n) \n";
     cin >> payrollEmp1.shift;
     cout << "Enter hours worked Monday: \n";
     cin >> payrollEmp1.monday;
     cout << "Enter hours worked Tuesday: \n";
     cin >> payrollEmp1.tuesday;
     cout << "Enter hours worked Wednesday: \n";
     cin >> payrollEmp1.wednesday;
     cout << "Enter hours worked Thursday: \n";
     cin >> payrollEmp1.thursday;
     cout << "Enter hours worked Friday: \n";
     cin >> payrollEmp1.friday;
     cout << "Enter hours worked Saturday: \n";
     cin >> payrollEmp1.saturday;
     cout << "Enter hours worked Sunday: \n";
     cin >> payrollEmp1.sunday;
}  
   payrollEmp1.hoursWorked = payrollEmp1.monday + payrollEmp1.tuesday + payrollEmp1.wednesday 
   + payrollEmp1.thursday + payrollEmp1.friday + payrollEmp1.saturday + payrollEmp1.sunday;     
  // payrollEmp1.income = (payrollEmp1.hoursWorked * payrollEmp1.payRate) 
  +payrollEmp1.overTimePay;
  // payrollEmp1.overTimePay = 1.5 * payrollEmp1.payRate * payrollEmp1.hoursWorked;

 for(int i=0; i<7; i++)
{
 payrollEmp1.income = payrollEmp1.payRate * payrollEmp1.hoursWorked;
 {
  (payrollEmp1.shift == 'N' || payrollEmp1.shift == 'n');
}
payrollEmp1.shift1 = 1.10 * payrollEmp1.payRate;   
}

if (payrollEmp1.hoursWorked > 40) 
{                            
    payrollEmp1.overTimePay = (payrollEmp1.hoursWorked - 40) * ( payrollEmp1.payRate * 1.5 );
}
else if(payrollEmp1.hoursWorked <= 40)
{
      payrollEmp1.income = payrollEmp1.hoursWorked * payrollEmp1.payRate);

}

payrollEmp1.income = payrollEmp1.overTimePay + payrollEmp1.regularPay; 

// payrollEmp1.wages = 40 * payrollEmp1.rate + 1.5 * payrollEmp1.rate * payrollEmp1.overtime);
// else payrollEmp1.wages = payrollEmp1.hours * payrollEmp1.rate




    //cout << "Name: "<< firstName << lastName << endl; 
    //cout << "Payrate: " << payRate << endl;
    //cout << "Hours worked: " << hoursWorked << endl;
    //cout << "Overtime Hours: " << overTimePay << endl;
    //cout << "Total income for the week: " << income << endl;

  cout << "              " << endl;
  cout << "Pay To The Order Of             " << payrollEmp1.firstName << endl;
  cout << " " << payrollEmp1.lastName << " $ " << payrollEmp1.income << endl;
  cout << "              " << << endl;
  //cout << "Pay To The Order Of: " << firstName << lastName << income <<endl;


 system ("pause");
 return 0;
 }

 }

2 个答案:

答案 0 :(得分:1)

此代码的语法错误:

for(int i=0; i<7; i++)
 payrollEmp1.income = payrollEmp1.payRate * payrollEmp1.hoursWorked;

  (payrollEmp1.shift == 'N' || payrollEmp1.shift == 'n')

for循环仅针对行payrollEmp1.income = ...执行,下一行(payrollEmp1.shift == 'N' || payrollEmp1.shift == 'n')没有意义。而且它的语法也不正确,因为它最后需要;

另一个语法错误出现在最后一行:

cout << "              " << << endl;

两个<<之间缺少某些内容或删除其中一个。

答案 1 :(得分:1)

如果在这里你错过了一个额外的支架

(payrollEmp1.shift == 'N' || payrollEmp1.shift == 'n')

    if (payrollEmp1.hoursWorked > 40) 
    {                            
        payrollEmp1.overTimePay = (payrollEmp1.hoursWorked - 40) * ( payrollEmp1.payRate * 1.5 );
    }
    else if(payrollEmp1.hoursWorked <= 40)
    {
        payrollEmp1.income = payrollEmp1.hoursWorked * payrollEmp1.payRate/* remove */) ;

    }

应该是

if(payrollEmp1.shift == 'N' || payrollEmp1.shift == 'n')
 {
    if (payrollEmp1.hoursWorked > 40) 
    {                            
        payrollEmp1.overTimePay = (payrollEmp1.hoursWorked - 40) * ( payrollEmp1.payRate * 1.5 );
    }
    else if(payrollEmp1.hoursWorked <= 40)
    {
        payrollEmp1.income = payrollEmp1.hoursWorked * payrollEmp1.payRate;

    }
}

也      cout&lt;&lt; “”&lt;&lt; &LT;&LT; ENDL; 应该      cout&lt;&lt; “”&lt;&lt; ENDL;