【问题标题】:Need help getting VOID code working w/ gross pay需要帮助让 VOID 代码在总工资的情况下工作
【发布时间】:2017-04-06 23:59:46
【问题描述】:

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

#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

是的,这是为了上课。不,我不想要答案。

疑似问题:

  • 我的 void 语法组织错误
  • 我的 while 语句没有正确使用
  • 我的变量被 void 函数非法调用

100% 感谢任何帮助我走上正轨的方法。

编辑:

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

void getGrossPay(int 常规, 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

【问题讨论】:

标签: c++ visual-c++ codeblocks c++builder


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    getGrossPay(regular);
    

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

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

    另外,你已经声明了DOUB_TIMEREG_RATETIME_HALF你的main()函数;因此,它在getGrossPay() 函数中是不可访问的。

    【讨论】:

      猜你喜欢
      • 2017-09-22
      • 2015-02-11
      • 2011-02-25
      • 1970-01-01
      • 2020-11-15
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多