因此,我正在创建一个函数来计算员工的工资,并输出工资以及他们工作的加班时间。
输出看起来像这样:
Enter hours worked (-1 to end): 41
Enter hourly rate of the worker ($00.00): 10.00
Employee worked 1 hour(s) overtime for a value of $15.00
Salary is $415.00
我唯一的问题是函数在上面的输出之后结束。 如何不断询问用户输入-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";
}
答案 0 :(得分:0)
您可以简单地将核心部分包装在while
循环中:
// 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";
}