计划未提供所需的输出

时间:2015-11-18 06:28:32

标签: c++

我有一项任务,我应该做以下事情。

  1. 编写一个程序,询问员工的工资和服务年限。
  2. 如果员工的服务年限超过10年。他们加薪10%。在5到10年之间,加薪是5%。其他人都会获得2%的加薪。
  3. 员工的年度奖金。每2年服务500美元。
  4. 以清晰专业的方式显示计算出的信息。
  5. 我将下面的代码编写为已分配问题的解决方案;但是我一直都会得到错误的输出。我试图调试程序来解决问题,我读了这本书并查看了网页,但我无法找出错误。

       #include <iostream>
        #include <iomanip>
    
    
    using std::cout;
    using std::cin;
    using std::setw;
    using std::setprecision;
    using std::fixed;
    using std::right;
    using std::left;
    using std::setfill;
    
    //function prototypes
    void GetInput(double & salary, int years_service);
    void CalcRaise(double & salary, int years_service);
    int CalcBonus(int years_service);
    void PrintCalculations(int years_service, double salary, int bonus);
    
    int main() 
    {
        //variable diclerations
        double salary = 0.00;
        int years_service = 0;
        int bonus = 0;
    
        //function calls
        GetInput(salary,years_service);
        CalcRaise(salary,years_service);
        CalcBonus(years_service);
        PrintCalculations(years_service,salary,bonus);
    
        cout << "\n";
        return 0;
    }
    
    //prompts the user for input
    void GetInput(double  &salary, int years_service)
    {
        cout << "Enter Salary: ";
        cin >> salary;
        cout << "\nEnter years of service: ";
        cin >> years_service;
    }
    
    //calculates the raise
    void CalcRaise(double & salary, int years_service)
    {
        double raise = 0.00;
    
    
        if (years_service > 10)
        {
            raise = salary * (10 / 100);
            salary = salary + raise;
        }
        else if (years_service >= 5 && years_service <= 10)
        {
            raise = salary * (5 / 100);
            salary = salary + raise;
        }
        else
        {
            raise = salary * (2 / 100);
            salary = salary + raise;
        }
    }
    
    //calculates the bonus
    int CalcBonus(int years_service)
    {
        int bonus = 0;
    
        bonus = (years_service / 2) * 500;
    
        return bonus;
    }
    
    //outputs the results of the calculations
    void PrintCalculations(int years_service, double salary, int bonus)
    {
        cout << setw(18) << left << "Years of Service" << setw(16) << left << " Salary after raise" 
            << setw(8) << left << " Bonus\n";
        cout << setw(15) << setfill(' ') << right<< years_service 
            << setw(18) << setfill(' ') << right << salary << setw(8) << right << setfill(' ') << bonus;
    }
    

    =========================

    The Output:
    
    Enter Salary: 4000
    
    Enter years of service: 7
    Years of Service   Salary after raise Bonus
                   0              4000       0
    

2 个答案:

答案 0 :(得分:2)

错误在这里:

void GetInput(double  &salary, int years_service)

您将years_service作为值传递。这将只复制你之前的0。然后你永远不会读它,但写信给它。原始值当然不会被覆盖。

您应该通过引用传递。

void GetInput(double  &salary, int& years_service)

答案 1 :(得分:1)

需要进行一些更改才能从代码中进行任何操作。

更改1

void GetInput(double & salary, int years_service);

应改为

void GetInput(double & salary, int &years_service);

更改2

您可以将CalcBonus的返回类型从int更改为double 你不会失去精确度,即

int CalcBonus(int years_service)
{
  int bonus = 0;

应改为

double CalcBonus(int years_service)
{
  double bonus = 0.0;

更改3

您没有将CalcBonus的返回值存储在任何地方 内部main函数声明奖金的双变量,例如奖励。 并捕获CalcBonus函数返回的值。

double bonus;
bonus = CalcBonus(years_service);

应该是它。

相关问题