未提供所需输出的程序

Programme not giving the required output

本文关键字:输出 程序      更新时间:2023-10-16

我有一个任务,应该在其中执行以下操作。

  1. 编写一个程序,询问员工的工资和服务年限
  2. 如果员工的服务年限超过10年。他们得到10%的加薪。在5到10年之间,加薪幅度为5%。其他人将获得2%的加薪
  3. 员工的年度奖金。每服务2年收费500美元
  4. 以清晰专业的方式显示计算的信息

我写了下面的代码作为分配问题的解决方案;然而,我总是得到错误的输出。我试着调试程序以找出问题,我读了书并在网上查找,但我找不出问题所在。

   #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 << " Bonusn";
    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

错误在这里:

void GetInput(double  &salary, int years_service)

years_service作为值传递。这将简单地复制您以前拥有的0。然后你永远不会读它,而是写它。原始值当然不会被覆盖。

您应该改为通过引用传递。

void GetInput(double  &salary, int& years_service)

需要进行一些更改才能对代码进行任何修改。

更改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函数中,为奖金声明一个双变量,比如bonus。并像这样捕获从CCD_ 4函数返回的值。

double bonus;
bonus = CalcBonus(years_service);

应该是这样。