需要帮助使 VOID 代码与总工资一起工作

Need help getting VOID code working w/ gross pay

本文关键字:一起 工作 代码 帮助 VOID      更新时间:2023-10-16

我需要帮助纠正以下代码的错误。它的主要功能是接收用户的工作小时数,然后根据工作小时数背后的逻辑,应用适当的小时费率。

#include <iostream>
#include <iomanip>
using namespace std;
//function prototype
void getGrossPay(int regular,
                 int &timeAndHalf,
                 int &doubleTime);
int main()
{   //variables
    const int REG_RATE = 10;
    const int TIME_HALF = 15;
    const int DOUB_TIME = 20;
    int hoursWorked = 0;
    cout << "Please enter your hours worked(press -1 to quit): ";
    cin >> hoursWorked;
    getGrossPay(regular, timeAndHalf, doubleTime);
    while (hoursWorked != -1);
    {
        if(hoursWorked >=1 && hoursWorked <=37)
        {
        getGrossPay(regular);
        cout << "Your gross pay is: $ (press -1 to exit)" << regular << endl;
        cout << "Please enter your hours worked(press -1 to quit): ";
        cin >> hoursWorked;
        }
        else
            if (hoursWorked >=38 && hoursWorked <=50)
        {
            getGrossPay(timeAndHalf);
            cout << "your gross pay is: $(press -1 to exit)" << timeAndHalf << endl;
            cout << "Please enter your hours worked(press -1 to quit): ";
            cin >> hoursWorked;
        }
            else
            if (hoursWorked >=50)
            {
            getGrossPay(doubleTime);
            cout << "your gross pay is: $(press -1 to exit)" << doubleTime << endl;
            cout << "Please enter your hours worked(press -1 to quit): ";
            cin >> hoursWorked;
            }//end if
    }//end while

}// end of main function

    //****function definitions*****
    void getGrossPay(int regular,
                 int &timeAndHalf,int &doubleTime)
    {
        regular = hoursWorked * REG_RATE;
        timeAndHalf = hoursWorked * TIME_HALF;
        doubleTime = hoursWorked * DOUB_TIME;
    } // end getGrossPay function

是的,这是给一堂课的。不,我不想要答案。

可疑问题:

  • 我的空白语法组织错误
  • 我的 while 语句未正确使用
  • 我的变量被非法从 void 函数调用

任何和所有帮助让我走上正轨的帮助都是 100% 赞赏的。

编辑:

所以这段代码"修复"让我得到一个基本上冻结的可执行文件:

void getGrossPay(int regular, int &timeAndHalf, int 和 doubleTime(;

const int REG_RATE = 10;
const int TIME_HALF = 15;
const int DOUB_TIME = 20;
int hoursWorked = 0;
int regular = 0;
int timeAndHalf = 0;
int doubleTime = 0;

int main(({//变量

cout << "Please enter your hours worked(press -1 to quit): " << endl;
cin >> hoursWorked;
while (hoursWorked != -1);
{
    if(hoursWorked >=1 && hoursWorked <=37)
    {
    getGrossPay(regular, timeAndHalf, doubleTime);
    cout << "Your gross pay is: $ (press -1 to exit)" << regular << endl;
    cout << "Please enter your hours worked(press -1 to quit): " << endl;
    cin >> hoursWorked;
    }
    else
        if (hoursWorked >=38 && hoursWorked <=50)
    {
        getGrossPay(regular, timeAndHalf, doubleTime);
        cout << "your gross pay is: $(press -1 to exit)" << timeAndHalf << endl;
        cout << "Please enter your hours worked(press -1 to quit): " << endl;
        cin >> hoursWorked;
    }
        else
        if (hoursWorked >=50)
        {
        getGrossPay(regular, timeAndHalf, doubleTime);
        cout << "your gross pay is: $(press -1 to exit)" << doubleTime << endl;
        cout << "Please enter your hours worked(press -1 to quit): " << endl;
        cin >> hoursWorked;
        }//end if
}//end while

}//主函数结束

//****function definitions*****
void getGrossPay(int regular,
             int &timeAndHalf,int &doubleTime)
{
    regular = hoursWorked * REG_RATE;
    timeAndHalf = hoursWorked * TIME_HALF;
    doubleTime = hoursWorked * DOUB_TIME;
} // end getGrossPay function

如果 GetGrossPay 被原型化为具有三个参数的函数,那么如果不给它三个参数,它将无法工作。我建议修改你的函数。

根据

定义,getGrossPay()是用 3 个参数声明的。但是,在 main() 函数中的 3 个实例中,例如:

getGrossPay(regular);

只有一个参数传递给函数。您需要重新处理函数,因为代码中有许多关于getGrossPay()的逻辑错误。

此外,您没有在 main() 中声明regular,而是将其传递给 getGrossPay() 函数。

此外,您已经在main()函数声明了DOUB_TIMEREG_RATETIME_HALF;因此,它在getGrossPay()函数中无法访问。