2015-11-03 115 views
0

以下是我迄今爲止的內容。我似乎無法弄清楚我做錯了什麼。我認爲我的輸入驗證功能也不正確。原始問題是:計算員工使用函數的淨工資 - C++

寫一個程序,計算員工的淨工資。該程序應提示用戶輸入工作小時數和小時工資。員工支付17.5%的工資總額作爲社會保障。所有員工都支付20%的工資總額。工資總額低於2500美元的員工需支付工資總額的10%稅。你的程序應該使用以下功能;

a。 computeGross:此函數根據工作小時數和小時工資計算總薪水。計算的工資總額返回到主要功能。

b。 computeDeductions:此函數接受總工資作爲輸入,計算社會保障和稅收減免,並將總扣減額返回至主要功能

c。 computeNet:該函數接受總工資,扣除和打印總工資,總扣減額和淨工資。 d)。 validateHours:該函數用於輸入驗證。它確定用戶提供的小時數是否有效。在支付期間工作的小時數不能超過150,不能是負數。

e。 validateWage:該函數用於輸入驗證。它決定了用戶提供的小時工資是否有效。小時工資不得超過200,不能爲負數。

#include <iostream> 
#include <iomanip> 
using namespace std; 

// Declare Functions 
double computeGross(double hoursWorked, double hourlyWage); 
double computeDeductions(double grossPay); 
double computeNet(double grossPay, double deductions); 
void validateHours(double hoursWorked); 
void validateWage(double hourlyWage); 

int main() 
{ 
    // Declare Variables 
    double hoursWorked = 0; 
    double hourlyWage = 0; 
    double grossPay = 0; 
    double deductions = 0; 
    double netSalary = 0; 

// Get the hours worked and hourly wage 
cout << "Please enter the amount of hours worked (HH.MM): " << endl; 
cin >> hoursWorked; 
cout << "Please enter in your hourly wage: $" << endl; 
cin >> hourlyWage; 

// Output the results 
cout << fixed << setprecision(2) 
    << "The net salary is: $" << netSalary << endl; 

    return 0; 
} 

// compteGross() function - get gross salary based on hours worked and hourly wage. 
double computeGross(double grossPay, double hoursWorked, double hourlyWage) 
{ 
return grossPay = hoursWorked * hourlyWage; 
} 

// computeDeductions() function - gets salary and calculates deductions 
double computeDeductions(double deductions, double grossPay) 
{ 

    if(grossPay < 2500) 
    { 
    deductions = (grossPay * .10) * .175; 
    } 
    else 
    { 
    deductions = (grossPay * .20) * .175; 
    } 

return deductions; 

} 

// computeNet() function - prints out gross salary,total deductions and net salary 
double computeNet(double netSalary, double grossPay, double deductions) 
{ 
netSalary = grossPay - deductions; 

cout << "The gross salary is: $" << grossPay << endl; 
cout << "The total deductions are: $" << deductions << endl; 
cout << "The net salary is: $" << netSalary << endl; 
return netSalary; 
} 

// validateHours() function - input validation; hours worked can;t exceed 150 or be neg. 
void validateHours(double hoursWorked) 
{ 
    if(hoursWorked < 0 || hoursWorked > 150) 
    { 
     cout << "Error! Hours can't be negative or exceed 150\n"; 
    } 
} 

// validateWage() - Input validation; wage can't exceed 200 or be negative 
void validateWage(double hourlyWage) 
{ 
    if(hourlyWage < 0 || hourlyWage > 200) 
    { 
    cout << "Error! Wage can't be negative or exceed 200\n"; 
    } 
} 
+2

那你的*具體*問題是什麼? – OldProgrammer

+0

當我運行程序時,我得到0作爲淨薪水,並且不,我的輸入不是0.我試圖弄清楚爲什麼是這樣。 – bradym55

+1

請花點時間瞭解如何使用調試器。步驟通過代碼,檢查變量等 – OldProgrammer

回答

2

首先,你必須在你的主要函數中實際調用你的函數來完成任何事情。

例如:

int main(){ 
    // Declare Variables 
    double hoursWorked = 0; 
    double hourlyWage = 0; 
    double grossPay = 0; 
    double deductions = 0; 
    double netSalary = 0; 

    // Get the hours worked and hourly wage 
    cout << "Please enter the amount of hours worked (HH.MM): " << endl; 
    cin >> hoursWorked; 
    cout << "Please enter in your hourly wage: $" << endl; 
    cin >> hourlyWage; 

    //you have to actually call your functions lol: 

    validateHours (hoursWorked); 

    validateWage(hourlyWage); 

    grossPay = computeGross(grossPay, hoursWorked, hourlyWage); 

    deductions = computeDeductions(grossPay); 

    netSalary = computeNet(netSalary,grossPay, deductions); 

    // Output the results 
    cout << fixed << setprecision(2) 
    << "The net salary is: $" << netSalary << endl; 

    return 0; 
} 

此外,當您在主函數之前聲明你的功能,你必須確保他們擁有相同數量的參數,實際上當你以後創建的函數體。

比如:以後

double computeNet(double grossPay, double deductions); 

然後使函數體,當你有這樣的: 您在一開始定義該

double computeNet(double netSalary, double grossPay, double deductions) 

你也不能在用戶定義的函數清點伊莫,但你應該沒問題,這個任務。你有沒有學習過參考變量?它看起來像你試圖做的一些事情可以從中受益。

編輯:我想我固定它:

#include <iostream> 
#include <iomanip> 
using namespace std; 

// Declare Functions 
double computeGross(double hoursWorked, double hourlyWage); 
double computeDeductions(double grossPay); 
double computeNet(double grossPay, double deductions); 
void validateHours(double hoursWorked); 
void validateWage(double hourlyWage); 

int main() 
{ 
    // Declare Variables 
    double hoursWorked = 0; 
    double hourlyWage = 0; 
    double grossPay = 0; 
    double deductions = 0; 
    double netSalary = 0; 

    // Get the hours worked and hourly wage 
    cout << "Please enter the amount of hours worked (HH.MM): " << endl; 
    cin >> hoursWorked; 
    cout << "Please enter in your hourly wage: $" << endl; 
    cin >> hourlyWage; 

    //you have to actually call your functions lol: 

    validateHours (hoursWorked); 

    validateWage(hourlyWage); 

    grossPay = computeGross(hoursWorked, hourlyWage); 

    deductions = computeDeductions(grossPay); 

    netSalary = computeNet(grossPay, deductions); 

    // Output the results 
    cout << fixed << setprecision(2) 
    << "The net salary is: $" << netSalary << endl; 

    return 0; 
} 

// compteGross() function - get gross salary based on hours worked and hourly wage. 
double computeGross(double hoursWorked, double hourlyWage) 
{ 
    return hoursWorked * hourlyWage; 
} 

// computeDeductions() function - gets salary and calculates deductions 
double computeDeductions(double grossPay) 
{ 
    double deductions; 
    if(grossPay < 2500) 
    { 
     deductions = (grossPay * .10) * .175; 
    } 
    else 
    { 
     deductions = (grossPay * .20) * .175; 
    } 

    return deductions; 

} 

// computeNet() function - prints out gross salary,total deductions and net salary 
double computeNet(double grossPay, double deductions) 
{ 
    double netSalary; 
    netSalary = grossPay - deductions; 

    cout << "The gross salary is: $" << grossPay << endl; 
    cout << "The total deductions are: $" << deductions << endl; 
    cout << "The net salary is: $" << netSalary << endl; 
    return netSalary; 
} 

// validateHours() function - input validation; hours worked can;t exceed 150 or be neg. 
void validateHours(double hoursWorked) 
{ 
    if(hoursWorked < 0 || hoursWorked > 150) 
    { 
     cout << "Error! Hours can't be negative or exceed 150\n"; 
    } 
} 

// validateWage() - Input validation; wage can't exceed 200 or be negative 
void validateWage(double hourlyWage) 
{ 
    if(hourlyWage < 0 || hourlyWage > 200) 
    { 
     cout << "Error! Wage can't be negative or exceed 200\n"; 
    } 
} 
0

你從來沒有真正叫你的任何功能。在收到用戶的最終輸入後​​,您應該開始調用函數來計算結果,然後它應該可以工作。

它只打印0的原因是因爲您將netSalary設置爲0,因此從未做過任何操作將變量操作爲任何其他值。