以下是我到目前为止的情况。我似乎无法弄清楚我做错了什么。我认为我的输入验证功能也是错误的。原来的问题是:
为程序编写一个计算员工净工资的程序。该程序应提示用户输入工作小时数和小时工资。员工支付其工资总额的17.5%作为社会保障缴款。所有员工的工资总额都要缴纳20%的税。工资总额低于2500的员工,对工资总额征收10%的税。您的程序应使用以下功能;
一个。 computeGross:此函数根据工作小时数和小时工资计算工资总额。计算的工资总额将返回主要职能部门。
湾computeDeductions:此函数接受总薪水作为输入,计算社会保障和税收减免并将总扣除额返回给主函数
℃。 computeNet:此函数接受工资总额,扣除额和打印输出工资总额,扣除总额和净工资。
d。 validateHours:此函数用于输入验证。它确定用户提供的小时数是否有效。支付期间的工作小时数不能超过150小时,也不能为负数。
即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";
}
}
答案 0 :(得分: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;
}
此外,当您在main函数之前声明函数时,您必须确保在以后实际创建函数体时它们具有相同数量的参数。
例如: 您可以在开头定义:
double computeNet(double grossPay, double deductions);
然后你在制作函数体时会有这个:
double computeNet(double netSalary, double grossPay, double deductions)
你也不应该在用户定义的函数imo中进行cout,但是对于这个赋值你应该没问题。您是否了解了参考变量?看起来你想要做的一些事情可以从中受益。
编辑:我想我修好了:
#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";
}
}
答案 1 :(得分:0)
你从未真正调用任何功能。在收到用户的最终输入后,您应该开始调用函数来计算结果,然后它应该可以工作。
它只输出0的原因是因为你将netSalary设置为0,然后从未做任何操作将变量操作为任何其他值。