未提供所需输出的程序
Programme not giving the required output
我有一个任务,应该在其中执行以下操作。
- 编写一个程序,询问员工的工资和服务年限
- 如果员工的服务年限超过10年。他们得到10%的加薪。在5到10年之间,加薪幅度为5%。其他人将获得2%的加薪
- 员工的年度奖金。每服务2年收费500美元
- 以清晰专业的方式显示计算的信息
我写了下面的代码作为分配问题的解决方案;然而,我总是得到错误的输出。我试着调试程序以找出问题,我读了书并在网上查找,但我找不出问题所在。
#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);
应该是这样。
相关文章:
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 显示错误输出的简单数组排序程序
- 更改.cpp程序的输入文件中数据的位置会意外更改输出
- 如何从Windows应用程序输出到标准?
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何使用C++读取另一个程序中的源代码输出
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 为什么这个程序的输出不如预期
- 在由Sublime文本3编译后在cmd上显示Java程序输出
- C++ 每次运行程序时我都会"nan"输出的问题
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 而循环:简单的除法程序输出零,不明白为什么
- 如何在 html 页面中插入 cgi 应用程序的输出?
- C 中的输出程序无明显的原因是白色的
- C++:更改简单输出程序的字体
- C++ 简单的输入/输出程序 + 枚举器
- C++字母等效数字代码输出程序
- 编译输出程序集