c++ - for 循环错误地输出指数

标签 c++ function for-loop

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

You are to write a program that will process employees and their pay. For each employee the program will read in an employee’s name and hourly pay rate. It should also read in the number of hours worked each day for 5 days and calculate his or her total number of hours worked. You must read the hours using a loop. The program should output the employee’s name, gross pay, total withholding amount and net pay."
Withholding is made up of state tax, federal tax, and FICA. For the purposes of this program the state tax will be 1.25% of the gross pay. FICA will be 7.65% of the gross pay. Federal tax will be 15% of the gross pay if the gross pay is under $500 and 25% otherwise.
You do not know how many employees there are but there will be a sentinel for the employee names. The sentinel is “done”

代码编译但输出总是大指数。感谢您的任何建议。

#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”和“net”(...),您可能不应该将它们作为未初始化的变量传递。您的意思是让它们出来(例如引用)还是它们之前已初始化?

关于c++ - for 循环错误地输出指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20364836/

相关文章:

php - 不同查询返回相同计数值

css - 无法在 SASS 中创建动态阴影调色板

javascript - 如何从 for 循环中的回调内部中断

c++ - PyQt : how to handle auto-resize of widgets when their content changes

c++指针从未初始化仍然输出为已初始化

c - 为什么函数调用没有被执行?

ios - 如何使用Swift SpriteKit在For循环中一一创建SKSprite节点

python - 如何将for循环中的结果写入多个CSV文件

C++ boost::regex 倍数捕获

C++ 模板 : What happens if we put wrong thing in our template class