for-loop错误地输出指数

时间:2013-12-04 00:58:23

标签: c++ function for-loop

我在使用这段代码时遇到了很多麻烦。作业是:

  

您要编写一个程序来处理员工及其薪酬。对于每个员工,该计划将读取员工的姓名和每小时工资率。它还应该读取每天工作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

3 个答案:

答案 0 :(得分:1)

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

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

也许你的意思是:

pay = calcPay(rate, sum);

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

答案 1 :(得分:0)

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

net

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

答案 2 :(得分:0)

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