C++ C++;计算员工工资的函数

C++ C++;计算员工工资的函数,c++,C++,所以我在做一个函数,计算员工的工资,并输出工资和他们加班的时间 输出如下所示: 输入工作小时数(-1到结束):41 输入工人的小时工资($00.00):10.00 员工加班1小时,价值15.00美元 薪水是415美元 我唯一的问题是函数在上面的输出之后结束。 在输入a-1之前,我如何持续询问用户工作小时数? 这就是我所拥有的: #include <iostream> int main() { double salary; int hours; int ov

所以我在做一个函数,计算员工的工资,并输出工资和他们加班的时间

输出如下所示:

输入工作小时数(-1到结束):41
输入工人的小时工资($00.00):10.00
员工加班1小时,价值15.00美元
薪水是415美元
我唯一的问题是函数在上面的输出之后结束。 在输入a-1之前,我如何持续询问用户工作小时数? 这就是我所拥有的:

#include <iostream>

int main()
{
    double salary;
    int hours;
    int overtime;
    double rate;
    int work_limit = 40;
    double overtimePay;

    std::cout << "Enter hours worked (-1 to end): ";
    std::cin >> hours;

    if(hours < 0);
    std::cout << "Enter hourly rate of worker ($00.00): ";
    std::cin >> rate;

    overtime = hours - work_limit;
    overtimePay = (overtime * rate) + (0.5 * rate * overtime);

    if(hours > work_limit)
        std::cout << "Employee worked " << overtime << " hour(s overtime for a value of $" << overtimePay << std::endl;

    salary = hours * rate;

    salary = (work_limit *  rate) + (overtime * rate * 1.5);
    std::cout << "Salary is: $" << salary << "\n\n";
}
#包括
int main()
{
双薪;
整小时;
国际加班;
双倍费率;
int work_limit=40;
双倍超期工资;
std::cout>小时;
如果(小时<0);
std::cout>速率;
加班=工时-工作限制;
加班工资=(加班费*费率)+(0.5*费率*加班费);
如果(小时数>工作限制)

std::cout您可以简单地将核心部分包装在
中,而
循环:

// loop infinitely
// exit from loop happens via if check right below
while(true){
    std::cout << "Enter hours worked (-1 to end): ";
    std::cin >> hours;

    // check wether the user wants to end
    // and if so, break out of the loop
    if (hours == -1) {
        break;
    }

    std::cout << "Enter hourly rate of worker ($00.00): ";
    std::cin >> rate;

    overtime = hours - work_limit;
    overtimePay = (overtime * rate) + (0.5 * rate * overtime);

    if(hours > work_limit)
        std::cout << "Employee worked " << overtime << " hour(s overtime for a value of $" << overtimePay << std::endl;

    salary = hours * rate;

    salary = (work_limit *  rate) + (overtime * rate * 1.5);
    std::cout << "Salary is: $" << salary << "\n\n";
}
//无限循环
//通过下面的if检查退出循环
while(true){
std::cout>小时;
//检查用户是否希望结束
//如果是这样的话,打破循环
如果(小时==-1){
打破
}
std::cout>速率;
加班=工时-工作限制;
加班工资=(加班费*费率)+(0.5*费率*加班费);
如果(小时数>工作限制)

std::不能使用
while
do while
循环
加班=小时-工作限制;
在计算之前,你应该先检查。我的意思是,如果小时数小于工作限制,会发生什么?编辑:可能将计算向下移动到检查后,并在该if中创建一个块。我的意思是
如果(小时>工作限制){计算并打印加班时间}
我修复了缺少的
,但是如果(小时数<0);
添加一点解释也会有帮助;)