我有一個任務,我應該做以下事情。程序沒有提供所需的輸出
- 寫一個程序,要求僱員的工資和服務年限。
- 如果員工有超過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 << " 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
嘗試在這樣的計算中使用'double'文字:'(10.0/100.0)' –
您需要通過引用'GetInput'傳遞'years_service'。 'void GetInput(double&salary,int&years_service);''現在''years_service'在'GetInput'中得到本地修改,並在'main'中保持爲'0'。 –
從小而簡單的東西開始,完美地工作(如打印「5」的程序)。一次增加一點複雜性(例如,使用戶輸入「工資」的值,然後輸出該值)。獨立開發新功能,並且只在兩者功能完美時才集成兩個功能。小步驟。 – Beta