【问题标题】:for-loop incorrectly outputs exponentsfor 循环错误地输出指数
【发布时间】: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 的建议

标签: c++ function for-loop


【解决方案1】:

您似乎没有初始化“支付”的值。

我错过了你初始化它的地方吗?

也许你的意思是:

pay = calcPay(rate, sum);

等等

另外:如果您在输出(...)中计算“with”、“gross”和“net”,您可能不应该将它们作为未初始化的变量传递。您的意思是让它们出来(例如参考)还是它们之前被初始化?

【讨论】:

  • 只是pay。变量withgrossnet 都是在output() 函数中计算的(它们不应该作为参数传入——它们应该只是函数中的局部变量)。
  • 对。所以它们也不应该被传递到函数中。
【解决方案2】:

在代码中变量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;
}

输出给出指数,因为在代码中明确放置了&lt;&lt;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';
}

【讨论】:

  • 别忘了选择最佳答案。
【解决方案3】:

您忘记在 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';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多