2015-11-18 36 views
0

我有一個任務,我應該做以下事情。程序沒有提供所需的輸出

  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 << " 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 
+0

嘗試在這樣的計算中使用'double'文字:'(10.0/100.0)' –

+2

您需要通過引用'GetInput'傳遞'years_service'。 'void GetInput(double&salary,int&years_service);''現在''years_service'在'GetInput'中得到本地修改,並在'main'中保持爲'0'。 –

+0

從小而簡單的東西開始,完美地工作(如打印「5」的程序)。一次增加一點複雜性(例如,使用戶輸入「工資」的值,然後輸出該值)。獨立開發新功能,並且只在兩者功能完美時才集成兩個功能。小步驟。 – Beta

回答

2

的錯誤是在這裏:

void GetInput(double &salary, int years_service) 

您通過years_service作爲值。這隻會複製你以前的0。然後你永遠不會讀它,但寫信給它。原始值當然不會被覆蓋。

您應該通過引用來代替。

void GetInput(double &salary, int& years_service) 
+0

謝謝你做了關於多年輸出的技巧 – ashumrani

1

需要進行一些更改才能使代碼生成任何內容。

變化1

void GetInput(double & salary, int years_service); 

應改爲

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

變化2

你可以從int更改CalcBonus的返回類型增加一倍,這樣 你不會失去精度,即

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); 

應該是這樣。

+0

請記住,儘管Stack Overflow不是一個免費的在線免費修復公司,在您發佈問題之前,嘗試將邏輯錯誤捕獲到最佳容量, – sjsam

+0

感謝您的幫助。同樣,感謝您提供關於堆棧溢出的提示。 – ashumrani