【发布时间】:2013-12-04 00:58:22
【问题描述】:
我在处理这段代码时遇到了很多麻烦。作业是:
您将编写一个程序来处理员工及其工资。对于每位员工,该程序将读取员工的姓名和小时工资率。它还应该读入连续 5 天每天工作的小时数,并计算他或她的总工作小时数。您必须使用循环读取小时数。程序应输出员工姓名、工资总额、预扣税总额和净工资。”
预扣税由州税、联邦税和 FICA 组成。就本计划而言,州税将为总工资的 1.25%。 FICA 将是总工资的 7.65%。如果总工资低于 500 美元,联邦税将为总工资的 15%,否则为 25%。
您不知道有多少员工,但会有员工姓名的哨兵。哨兵“完成”
代码可以编译,但是输出总是大指数。谢谢你的建议。
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
void instructions(string &name, float &rate, float &sum);
string getName();
float getRate();
float getHours();
float calcPay(float rate, float sum);
float calcGross(float pay);
float calcAmount(float gross);
float calcNet(float gross, float with);
void output(string name, float rate, float sum, float with, float gross, float net, float\
pay);
int main()
{
string name, done;
int counter;
float rate, sum, gross, with, pay, net;
instructions(name, rate, sum);
calcPay(rate, sum);
output(name, rate, sum, with, gross, net, pay);
return 0;
}
void instructions(string &name, float &rate, float &sum)
{
name = getName();
rate = getRate();
sum = getHours();
}
string getName()
{
string name, done;
cout<<"Enter the name of the employee: "<<" ";
cin>>name;
if (name == done)
{
cout <<"bye!";
}
return name;
}
float getRate()
{
float rate;
cout<<"Enter the hourly pay rate: "<<" ";
cin>>rate;
return rate;
}
float getHours()
{
int counter;
float hours, sum=0;
for (counter=1; counter <=5; counter++)
{
cout<<"Enter the number of hours worked for Day "<<counter<<":"<<" ";
cin>> hours;
sum += hours;
}
return sum;
}
float calcPay(float rate, float sum)
{
float pay;
pay = rate * sum;
return pay;
}
float calcGross (float pay)
{
float gross;
gross = (pay * .89);
return gross;
}
float calcAmount(float gross)
{
float with;
if (gross >500)
with = (gross *.15);
else
with = (gross *.25);
return with;
}
float calcNet(float gross, float with)
{
float net;
net = gross - with;
return net;
}
void output(string name, float rate, float sum, float with, float gross, float net, float\
pay)
{
gross = calcGross(pay);
with = calcAmount(gross);
net = calcNet(gross, with);
cout<<"Payroll"<<'\n';
cout<<"======================================="<<'\n';
cout<<"Employee name: "<<name<<'\n';
cout<<"Gross pay: $ "<<setprecision(2)<<gross<<'\n';
cout<<"Total Withholding: $ "<<setprecision(2)<<with<<'\n';
cout<<"Net pay: $ "<<setprecision(2)<<net<<'\n';
}
示例运行:
Enter the name of the employee: Alice
Enter the hourly pay rate: 7.75
Enter the number of hours worked for Day 1: 5
Enter the number of hours worked for Day 2: 6
Enter the number of hours worked for Day 3: 5
Enter the number of hours worked for Day 4: 4
Enter the number of hours worked for Day 5: 5
Payroll
=======================================
Employee name: Alice
Gross pay: $ -1.9e+38
Total Withholding: $ -4.8e+37
Net pay: $ -1.4e+38
【问题讨论】:
-
代码太多。将其缩小到一个特定的问题和代码段。
-
你能举一个输入输出的例子吗?
-
您是否尝试过使用已知输入独立测试您的每一种方法,看看是否获得了预期的输出?这可能是学习如何使用环境调试器的好时机,以帮助您逐步执行代码并跟踪问题。
-
我知道它有很多代码。我尝试将其拆分为单独的函数以尝试组织它。
-
添加了输入/输出.. 感谢 Alan Ill 的建议