关于C ++ Payroll

时间:2013-09-04 00:58:23

标签: c++

我正在参加在线C ++课程,我很难学习。我不确定我的代码对于下面的问题我究竟做错了什么。如果小时数= 40,我会得到正确的公式,但如果小于40或小于40,则会出现问题。 我感谢您的帮助! 干杯, R上。

Problem:
if hrs <= 40 the regular pay = hrs times pay rate
if hrs > 40 then overtime pay = 1.5 times (hrs - 40) times pay rate
gross pay = regular pay plus overtime pay

// my code

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    //variable declarations

    int EmployeeIdentificationNumber = 0;
    double Hours = 0;
    double PayRate = 0;
    double GrossPay = 0;
    double RegularPay = 0;
    double OvertimePay = 0;


   std::cout << "Welcome to the Employee Payroll.\n"; // display message

    std:: cout << "Enter Your Employee Identification Number: "; //promp user for data
    std::cin >> EmployeeIdentificationNumber; //read integer from user into             EmployeeIdentificationNumber

    std::cout << "Please enter Hours worked: " ; // prompt user for data
    std::cin >> Hours; //read integer from user into Hours
    std::cout << "Please enter Pay Rate: " ; // prompt user for data
    std::cin >> PayRate; //read integer from user into PayRate

    RegularPay = Hours * PayRate; //calculate RegularPay
    OvertimePay = 1.5 * (Hours - 40) * PayRate; //Calculate Overtime

    //Qualifier for RegularPay

    if (Hours <= 40);
    RegularPay = Hours * PayRate;
   OvertimePay = 0;
   GrossPay = RegularPay + OvertimePay;
   std::cout << "Gross Pay is = $" ;

    //Qualifier for OverTime

    if (Hours > 40);
    RegularPay = Hours * PayRate;
    OvertimePay = 1.5 * (Hours - 40) * PayRate;
      GrossPay = RegularPay + OvertimePay;
    std::cout << RegularPay + OvertimePay << std::endl;

    std::cout << "Thanks for using the Employee Payroll\n";
    system("PAUSE");
  return EXIT_SUCCESS;

}

3 个答案:

答案 0 :(得分:7)

关于您的代码段:

if (Hours <= 40);
    RegularPay = Hours * PayRate;
    OvertimePay = 0;

这不符合你的想法。 ;语句末尾的if表示“如果小时数小于40,则不执行任何操作”,则无论如何都设置常规和加班。你可能想要的是:

if (Hours <= 40) {
    RegularPay = Hours * PayRate;
    OvertimePay = 0;
}

常规和加班工资的整体计算可能更好地写成:

if (Hours <= 40) {
    RegularPay = Hours * PayRate;
    OvertimePay = 0;
} else {
    RegularPay = 40.0 * PayRate;
    OvertimePay = 1.5 * (Hours - 40) * PayRate;
}

GrossPay = RegularPay + OvertimePay;
std::cout << "Gross Pay is = $"  << GrossPay << '\n';

您可以看到为这两种情况设置了常规和加班费的正确值,之后您可以添加它们并以任何您想要的方式打印它们。

请记住这一点(在RegularPay = 40.0 * PayRate条款中使用else)是因为加班时间通常是时间的一半。

如果您在一个两倍半的行业工作(即,您很幸运),请根据您的原始版本将计算更改为RegularPay = Hours * PayRate。这似乎是您的描述指定的方式,但您可能需要咨询您的导师或至少评论推理。

如果两倍半,你可以将代码简化为:

RegularPay = Hours * PayRate;
OvertimePay = 0;
if (Hours > 40)
    OvertimePay = 1.5 * (Hours - 40) * PayRate;

GrossPay = RegularPay + OvertimePay;
std::cout << "Gross Pay is = $"  << GrossPay << '\n';

答案 1 :(得分:2)

首先,你不应该写:

     if(Hours <= 40);

因为if语句后面的分号不允许执行其余代码(要在语句后执行)。你应该把if之后的其余代码放在花括号

答案 2 :(得分:0)

将“Gross Pay is = $”打印在外面,因为这两种情况都很常见。使用开始大括号而不是分号,在代码块的代码块末尾使用结束花括号。代码应该是:

std::cout << "Gross Pay is = $" ;

//Qualifier for RegularPay

if (Hours <= 40){
    RegularPay = Hours * PayRate;
    OvertimePay = 0;
    GrossPay = RegularPay + OvertimePay;
}

//Qualifier for OverTime

if (Hours > 40){
    RegularPay = Hours * PayRate;
    OvertimePay = 1.5 * (Hours - 40) * PayRate;
    GrossPay = RegularPay + OvertimePay;
}
std::cout << RegularPay + OvertimePay << std::endl;