-2

我正在参加在线 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;

}

4

3 回答 3

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 * PayRateelse子句中的使用)是加班时间的一个半,因为这通常是这种情况。

如果您在一个两倍半的行业工作(即,您非常幸运),请将计算更改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';
于 2013-09-04T01:03:41.517 回答
2

首先,你不应该写:

     if(Hours <= 40);

因为语句后的分号if不允许执行其余代码(您希望在语句之后执行)。你应该把剩下的代码if放在花括号里

于 2013-09-04T01:06:37.297 回答
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;
于 2013-09-04T01:12:41.517 回答