user3063730
user3063730

Reputation: 15

for-loop incorrectly outputs exponents

I'm having a lot of trouble with this code. The assignment is:

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”

The code compiles however the outputs are always large exponents. Thank you for any advice.

#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';
}

Sample run:

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

Upvotes: 1

Views: 606

Answers (3)

MinandLucy
MinandLucy

Reputation: 76

You forget initialize the varialble in main function. just modify your program like this, then it could run.

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';
}

Upvotes: 0

Alexander Myshov
Alexander Myshov

Reputation: 3101

In the code variables pay, gross, pay, and net are not initialized. Due your code above it would be like this:

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;
}

The output give exponents, because in code explicitly placed <<setprecision(2). Due human-readability I just omitted precision in the code below:

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';
}

Upvotes: 0

FuzzyBunnySlippers
FuzzyBunnySlippers

Reputation: 3397

It does not appear you are initializing the value for "pay".

Am I missing where you have initialized it?

Perhaps you meant:

pay = calcPay(rate, sum);

etc.

ALSO: If you are calculating "with", "gross", and "net" in the output(...), you probably should not be passing them in as uninitialized variables. Did you mean for them to come out (e.g. reference) or were they passed in initialized before?

Upvotes: 1

Related Questions