for循环错误地输出指数



我在这段代码上遇到了很多麻烦。任务是:

你要写一个程序来处理员工和他们的工资。对于每个员工,该程序将读取一名员工的姓名和时薪。它还应读取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

您似乎没有初始化"pay"的值。

我是否缺少您初始化它的位置?

也许你的意思是:

pay = calcPay(rate, sum);

等等。

此外:如果您正在计算输出中的"with"、"gross"one_answers"net"(…),则可能不应该将它们作为未初始化的变量传入。你的意思是让它们出来(例如引用),还是它们之前被初始化了?

在代码中,变量paygrosspaynet未初始化。由于您上面的代码,它将是这样的:

int main()
{
    string name, done;
    int counter;
    float rate, sum, gross, with, pay, net;
    instructions(name, rate, sum);
    pay = calcPay(rate, sum);
    gross = calcGross(pay);
    with = calcAmount(gross);
    net = calcNet(gross, with);
    output(name, rate, sum, with, gross, net, pay);
    return 0;
}

输出给出了指数,因为在代码中明确放置了<<setprecision(2)。由于人类的可读性,我只是在下面的代码中省略了精度:

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:                            $ "<<gross<<'n';
    cout<<"Total Withholding:                    $ "<<with<<'n';
    cout<<"Net pay:                              $ "<<net<<'n';
}

您忘记初始化main函数中的变量。只要像这样修改你的程序,它就可以运行了。

int main()
{
    string name, done;
    int counter;
    float rate, sum, gross, with, pay, net;
    instructions(name, rate, sum);
    pay=calcPay(rate, sum);
    output(name, rate, sum, with, gross, net, pay);
    return 0;
}
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:                            $ "<<gross<<'n';
    cout<<"Total Withholding:                    $ "<<with<<'n';
    cout<<"Net pay:                              $ "<<net<<'n';
}

最新更新