# mystery variable in code

Thread Starter

#### Killerbee65

Joined May 15, 2017
248
Hello all,

So I'm back with another one of my professor's assignments. This time I'm supposed to calculate most of the information on a pay stub such as, base pay, overtime, taxes, etc. I'm currently reverse engineering my professor's code and came across this variable in a function that doesn't seem to have a value but is used as if it does, can someone please explain how the variable "gross" works?

also, can someone show me how to paste code correctly from visual studio? I'm new to it and don't really know how to use it.

C++:
#include <iostream>
#include <iomanip>
using namespace std;

/*
function that computes a pre-tax IRA (Individual Retirement Account) deduction from gross pay prior to computing the withholding amount
if gross pay is greater than $400 and less than$500, deduct 5%
if gross pay is $500 or more, deduct 10% ira = gross * percent_deduction; */ double computeIRA(double gross); /* function that computes federal withholding 0 dependents - withhold 28% of gross pay 1 dependents - withhold 20% of gross pay 2 dependents - withhold 18% of gross pay 3 dependents or more - withhold 15% of gross pay */ double computeFedWithholding(int dependent, double gross); int main() { //declaring variables int hrs = 0, reg_hrs = 0, ot_hrs = 0, dt_hrs = 0, dependents = 0; double payRate = 0, regPay = 0, otPay = 0, dtPay = 0, grossPay = 0, netPay, withholding; const short unsigned FULL_TIME = 40; const short unsigned OT_MAX = 50; const double OT_RATE = 1.5; const double DT_RATE = 2; const double NO_DEPENDENTS = 0.28, SINGLE_DEPENDENT = 0.20, TWO_DEPENDENTS = 0.18, THREE_DEPENDENTS = 0.15; bool isValid = false; // Get pay rate while (!isValid) { cout << "Enter your hourly rate of pay: "; cin >> payRate; isValid = payRate >= 0; if (!isValid) cout << "Error: the pay rate must be >= 0." << endl; } isValid = false; // reset the flag // Get hours worked /* Note: some people get confused with line 73 so I show here any more explicit */ while (isValid == false) { cout << "Enter the number of hours worked: "; cin >> hrs; isValid = hrs >= 0; if (!isValid) cout << "Error: the number of hours must be >= 0." << endl; } isValid = false; // reset the flag // Get hours worked do { cout << "Enter the number of dependents: "; cin >> dependents; isValid = dependents >= 0; if (!isValid) cout << "Error: the number of dependents must be >= 0." << endl; } while (isValid == false); /* NOTE: because we validate above we can rest assured that all calculation that follow should work-out */ // Compute DT hours dt_hrs = hrs - OT_MAX; /* Compute OT hours if we have double hours ot_hrs will be the difference between max OT and full-time (10) else use the same calculation from last week */ ot_hrs = hrs - FULL_TIME; if (dt_hrs > 0) { reg_hrs = FULL_TIME; ot_hrs - FULL_TIME; } else if (ot_hrs > 0) { dt_hrs = 0; reg_hrs = FULL_TIME; } else { dt_hrs = 0; ot_hrs = 0; reg_hrs = hrs; } //calculating the gross pay regPay = reg_hrs * payRate; otPay = ot_hrs * payRate * OT_RATE; dtPay = dt_hrs * payRate * DT_RATE; grossPay = regPay + otPay + dtPay; // compute double ira = computeIRA(gross); // IRA deduction double modifiedGross = gross - ira; // calculate the withholding withholding = computeFedWithholding(dependent, modifiedGross); // calculate the net pay netPay = modifiedGross - withholding; //displaying to the user the number of hours they worked, their hourly pay rate, and the calculated gross pay cout << endl << fixed << showpoint << setprecision(2); cout << "Regular hours at$" << payRate << " = " << reg_hrs <<  endl;
cout << "Base pay at regular rate = $" << regPay << endl; cout << "Overtime hours at time and a half = " << ot_hrs << endl; cout << "Overtime pay at time and a half =$" << otPay << endl;
cout << "Overtime hours at double time = " << ot_hrs << endl;
cout << "Overtime pay at doulbe time = $" << otPay << endl; cout << "Federal tax bracket with " << dependents << " dependents is " << setprecision(0) << noshowpoint << taxRate * 100 << "%" << endl; cout << setprecision(2) << showpoint; cout << "Gross pay =$" << grossPay << endl;
cout << "IRA contribution with " << grossPay << " in gross pay = " << ira << endl;
cout << "Modified Gross pay = $" << modifiedGross << endl; cout << "Federal tax withholding with " << dependents << " dependents is$" << withholding << endl;
cout << "Net pay = $" << netPay << endl; // display lab 5 specific display message return 0; } double computeIRA(double gross) { double ira = 0; if (gross > 400 && gross < 500) { ira = gross * 0.05; } else if (gross >= 500) { ira = gross * 0.10; } return ira; } /* function that computes federal withholding 0 dependents - withhold 28% of gross pay 1 dependents - withhold 20% of gross pay 2 dependents - withhold 18% of gross pay 3 dependents or more - withhold 15% of gross pay */ double computeFedWithholding(int dependent, double gross) { double taxRate, withholding; switch (dependents) { case 0: taxRate = NO_DEPENDENTS; break; case 1: taxRate = SINGLE_DEPENDENT; break; case 2: taxRate = TWO_DEPENDENTS; break; default: taxRate = THREE_DEPENDENTS; } withholding = grossPay * taxRate; return withholding; } Moderator edit: added code tags #### WBahn Joined Mar 31, 2012 26,398 It would be quite helpful if you would format the code to make it organized and readable and also say where the variable you are concerned about is first used. If you are talking about the part of the code are the IRA deduction is calculated, then I imagine that if you look at the few lines of code before it you will see where the professor probably made a mistake and what they likely meant. Thread Starter #### Killerbee65 Joined May 15, 2017 248 It would be quite helpful if you would format the code to make it organized and readable and also say where the variable you are concerned about is first used. If you are talking about the part of the code are the IRA deduction is calculated, then I imagine that if you look at the few lines of code before it you will see where the professor probably made a mistake and what they likely meant. I mentioned in the post I didn't know how to format it and telling you which line would just be a waste of time. I probably should have said something like "at the bottom" or something. and the reason I didn't touch is is for reference purposes if the problem was found. on the function all the way at the bottom it says "double computeIRA(double gross)" I'm good with knowing that bc I know now "gross" resides in the function, what trips me up is the fact "gross" doesn't have a value so how did he multiply it by 0.05? and in the next function, "gross" isn't even used. When I replicate the code and call it "fed_withhold(gross)" it says gross wasn't declared but I made sure to format my code so that it is similar to my professors (as a test to see if I could figure out how it would work) and whats even more confusing is when HE calls it he multiplies it again but gross doesn't have a value. I made sure the code words so either my calculations are wrong or there some black magic at work #### MrChips Joined Oct 2, 2009 23,080 You mean enclosing the code with code tags like this: [code=C] ... your code goes here [/code] Thread Starter #### Killerbee65 Joined May 15, 2017 248 You mean enclosing the code with code tags like this: [code=CPP] ... your code goes here [/code] I'm not sure, when I coded for Arduino it would have an option to copy and paste code for forums and it would be formatted, not sure how to do that with visual studio, let me see if it works. C++: #include <iostream> #include <iomanip> using namespace std; /* function that computes a pre-tax IRA (Individual Retirement Account) deduction from gross pay prior to computing the withholding amount if gross pay is greater than$400 and less than $500, deduct 5% if gross pay is$500 or more, deduct 10%

ira = gross * percent_deduction;
*/
double computeIRA(double gross);

/*
function that computes federal withholding
0 dependents - withhold 28% of gross pay
1 dependents - withhold 20% of gross pay
2 dependents - withhold 18% of gross pay
3 dependents or more - withhold 15% of gross pay
*/
double computeFedWithholding(int dependent, double gross);

int main()
{
//declaring variables
int hrs = 0, reg_hrs = 0, ot_hrs = 0, dt_hrs = 0, dependents = 0;
double payRate = 0, regPay = 0, otPay = 0, dtPay = 0, grossPay = 0, netPay, withholding;
const short unsigned FULL_TIME = 40;
const short unsigned OT_MAX = 50;
const double OT_RATE = 1.5;
const double DT_RATE = 2;
const double NO_DEPENDENTS = 0.28, SINGLE_DEPENDENT = 0.20, TWO_DEPENDENTS = 0.18, THREE_DEPENDENTS = 0.15;
bool isValid = false;

// Get pay rate
while (!isValid)
{
cout << "Enter your hourly rate of pay: ";
cin >> payRate;

isValid = payRate >= 0;
if (!isValid)
cout << "Error: the pay rate must be >= 0." << endl;
}

isValid = false; // reset the flag

// Get hours worked
/*
Note: some people get confused with line 73 so I show here any more explicit
*/
while (isValid == false)
{
cout << "Enter the number of hours worked: ";
cin >> hrs;

isValid = hrs >= 0;
if (!isValid)
cout << "Error: the number of hours must be >= 0." << endl;
}
isValid = false; // reset the flag

// Get hours worked
do
{
cout << "Enter the number of dependents: ";
cin >> dependents;

isValid = dependents >= 0;

if (!isValid)
cout << "Error: the number of dependents must be >= 0." << endl;
} while (isValid == false);

/*
NOTE: because we validate above we can rest assured that all calculation that follow should work-out
*/

// Compute DT hours
dt_hrs = hrs - OT_MAX;

/*
Compute OT hours
if we have double hours ot_hrs will be the difference between max OT and full-time (10)
else use the same calculation from last week
*/
ot_hrs = hrs - FULL_TIME;

if (dt_hrs > 0)
{
reg_hrs = FULL_TIME;
ot_hrs - FULL_TIME;
}
else if (ot_hrs > 0)
{
dt_hrs = 0;
reg_hrs = FULL_TIME;
}
else
{
dt_hrs = 0;
ot_hrs = 0;
reg_hrs = hrs;
}

//calculating the gross pay
regPay = reg_hrs * payRate;
otPay = ot_hrs * payRate * OT_RATE;
dtPay = dt_hrs * payRate * DT_RATE;
grossPay = regPay + otPay + dtPay;

// compute
double ira = computeIRA(gross);

// IRA deduction
double modifiedGross = gross - ira;

// calculate the withholding
withholding = computeFedWithholding(dependent, modifiedGross);

// calculate the net pay
netPay = modifiedGross - withholding;

//displaying to the user the number of hours they worked, their hourly pay rate, and the calculated gross pay
cout << endl << fixed << showpoint << setprecision(2);
cout << "Regular hours at $" << payRate << " = " << reg_hrs << endl; cout << "Base pay at regular rate =$" << regPay << endl;
cout << "Overtime hours at time and a half = " << ot_hrs << endl;
cout << "Overtime pay at time and a half = $" << otPay << endl; cout << "Overtime hours at double time = " << ot_hrs << endl; cout << "Overtime pay at doulbe time =$" << otPay << endl;
cout << "Federal tax bracket with " << dependents << " dependents is " << setprecision(0) << noshowpoint << taxRate * 100 << "%" << endl;
cout << setprecision(2) << showpoint;
cout << "Gross pay = $" << grossPay << endl; cout << "IRA contribution with " << grossPay << " in gross pay = " << ira << endl; cout << "Modified Gross pay =$" << modifiedGross << endl;
cout << "Federal tax withholding with " << dependents << " dependents is $" << withholding << endl; cout << "Net pay =$" << netPay << endl;

// display lab 5 specific display message

return 0;
}

double computeIRA(double gross)
{
double ira = 0;

if (gross > 400 && gross < 500)
{
ira = gross * 0.05;
}
else if (gross >= 500)
{
ira = gross * 0.10;
}

return ira;
}

/*
function that computes federal withholding
0 dependents - withhold 28% of gross pay
1 dependents - withhold 20% of gross pay
2 dependents - withhold 18% of gross pay
3 dependents or more - withhold 15% of gross pay
*/
double computeFedWithholding(int dependent, double gross)
{
double taxRate, withholding;
switch (dependents)
{
case 0:
taxRate = NO_DEPENDENTS;
break;
case 1:
taxRate = SINGLE_DEPENDENT;
break;
case 2:
taxRate = TWO_DEPENDENTS;
break;
default:
taxRate = THREE_DEPENDENTS;
}
withholding = grossPay * taxRate;

return withholding;
}
Moderator edit: using code=CPP

Thread Starter

#### Killerbee65

Joined May 15, 2017
248

#### MrChips

Joined Oct 2, 2009
23,080
Thread Starter

#### Killerbee65

Joined May 15, 2017
248
C++:
#include <iostream>
#include <iomanip>
using namespace std;

/*
function that computes a pre-tax IRA (Individual Retirement Account) deduction from gross pay prior to computing the withholding amount
if gross pay is greater than $400 and less than$500, deduct 5%
if gross pay is $500 or more, deduct 10% ira = gross * percent_deduction; */ double computeIRA(double gross); /* function that computes federal withholding 0 dependents - withhold 28% of gross pay 1 dependents - withhold 20% of gross pay 2 dependents - withhold 18% of gross pay 3 dependents or more - withhold 15% of gross pay */ double computeFedWithholding(int dependent, double gross); int main() { //declaring variables int hrs = 0, reg_hrs = 0, ot_hrs = 0, dt_hrs = 0, dependents = 0; double payRate = 0, regPay = 0, otPay = 0, dtPay = 0, grossPay = 0, netPay, withholding; const short unsigned FULL_TIME = 40; const short unsigned OT_MAX = 50; const double OT_RATE = 1.5; const double DT_RATE = 2; const double NO_DEPENDENTS = 0.28, SINGLE_DEPENDENT = 0.20, TWO_DEPENDENTS = 0.18, THREE_DEPENDENTS = 0.15; bool isValid = false; // Get pay rate while (!isValid) { cout << "Enter your hourly rate of pay: "; cin >> payRate; isValid = payRate >= 0; if (!isValid) cout << "Error: the pay rate must be >= 0." << endl; } isValid = false; // reset the flag // Get hours worked /* Note: some people get confused with line 73 so I show here any more explicit */ while (isValid == false) { cout << "Enter the number of hours worked: "; cin >> hrs; isValid = hrs >= 0; if (!isValid) cout << "Error: the number of hours must be >= 0." << endl; } isValid = false; // reset the flag // Get hours worked do { cout << "Enter the number of dependents: "; cin >> dependents; isValid = dependents >= 0; if (!isValid) cout << "Error: the number of dependents must be >= 0." << endl; } while (isValid == false); /* NOTE: because we validate above we can rest assured that all calculation that follow should work-out */ // Compute DT hours dt_hrs = hrs - OT_MAX; /* Compute OT hours if we have double hours ot_hrs will be the difference between max OT and full-time (10) else use the same calculation from last week */ ot_hrs = hrs - FULL_TIME; if (dt_hrs > 0) { reg_hrs = FULL_TIME; ot_hrs - FULL_TIME; } else if (ot_hrs > 0) { dt_hrs = 0; reg_hrs = FULL_TIME; } else { dt_hrs = 0; ot_hrs = 0; reg_hrs = hrs; } //calculating the gross pay regPay = reg_hrs * payRate; otPay = ot_hrs * payRate * OT_RATE; dtPay = dt_hrs * payRate * DT_RATE; grossPay = regPay + otPay + dtPay; // compute double ira = computeIRA(gross); // IRA deduction double modifiedGross = gross - ira; // calculate the withholding withholding = computeFedWithholding(dependent, modifiedGross); // calculate the net pay netPay = modifiedGross - withholding; //displaying to the user the number of hours they worked, their hourly pay rate, and the calculated gross pay cout << endl << fixed << showpoint << setprecision(2); cout << "Regular hours at$" << payRate << " = " << reg_hrs << endl;
cout << "Base pay at regular rate = $" << regPay << endl; cout << "Overtime hours at time and a half = " << ot_hrs << endl; cout << "Overtime pay at time and a half =$" << otPay << endl;
cout << "Overtime hours at double time = " << ot_hrs << endl;
cout << "Overtime pay at doulbe time = $" << otPay << endl; cout << "Federal tax bracket with " << dependents << " dependents is " << setprecision(0) << noshowpoint << taxRate * 100 << "%" << endl; cout << setprecision(2) << showpoint; cout << "Gross pay =$" << grossPay << endl;
cout << "IRA contribution with " << grossPay << " in gross pay = " << ira << endl;
cout << "Modified Gross pay = $" << modifiedGross << endl; cout << "Federal tax withholding with " << dependents << " dependents is$" << withholding << endl;
cout << "Net pay = $" << netPay << endl; // display lab 5 specific display message return 0; } double computeIRA(double gross) { double ira = 0; if (gross > 400 && gross < 500) { ira = gross * 0.05; } else if (gross >= 500) { ira = gross * 0.10; } return ira; } /* function that computes federal withholding 0 dependents - withhold 28% of gross pay 1 dependents - withhold 20% of gross pay 2 dependents - withhold 18% of gross pay 3 dependents or more - withhold 15% of gross pay */ double computeFedWithholding(int dependent, double gross) { double taxRate, withholding; switch (dependents) { case 0: taxRate = NO_DEPENDENTS; break; case 1: taxRate = SINGLE_DEPENDENT; break; case 2: taxRate = TWO_DEPENDENTS; break; default: taxRate = THREE_DEPENDENTS; } withholding = grossPay * taxRate; return withholding; } Thread Starter #### Killerbee65 Joined May 15, 2017 248 #### WBahn Joined Mar 31, 2012 26,398 I mentioned in the post I didn't know how to format it and telling you which line would just be a waste of time. I probably should have said something like "at the bottom" or something. and the reason I didn't touch is is for reference purposes if the problem was found. on the function all the way at the bottom it says "double computeIRA(double gross)" I'm good with knowing that bc I know now "gross" resides in the function, what trips me up is the fact "gross" doesn't have a value so how did he multiply it by 0.05? and in the next function, "gross" isn't even used. When I replicate the code and call it "fed_withhold(gross)" it says gross wasn't declared but I made sure to format my code so that it is similar to my professors (as a test to see if I could figure out how it would work) and whats even more confusing is when HE calls it he multiplies it again but gross doesn't have a value. I made sure the code words so either my calculations are wrong or there some black magic at work The "double computeIRA(double gross)" is a function definition and the "gross" is the name of the parameter that is passed into the function. So when it is called with, say myContribution = computeIRA(monthlySalary*12) the value obtained by computing monthlySalary*12 is passed to the function and copied into the local variable "gross". Where the undeclared parameter is coming from is line 118 and 121 (in your original post as it currently stands). Thread Starter #### Killerbee65 Joined May 15, 2017 248 The "double computeIRA(double gross)" is a function definition and the "gross" is the name of the parameter that is passed into the function. So when it is called with, say myContribution = computeIRA(monthlySalary*12) the value obtained by computing monthlySalary*12 is passed to the function and copied into the local variable "gross". Where the undeclared parameter is coming from is line 118 and 121 (in your original post as it currently stands). I think I understand, so you're saying that "gross" on line 121 is the local variable and just a result of the calculations made in the computeIRA function? also, what does ira represent on lines 118 and 121? Thread Starter #### Killerbee65 Joined May 15, 2017 248 Also I'm a visual learner so my apologies if I ask a lot of questions Thread Starter #### Killerbee65 Joined May 15, 2017 248 So this is my attempt at the code, sorry if it's hard to read I had to rearrange a lot of stuff but what I mainly want to focus on is the number of dependants or "dep", on line 94-ish(i had a few lines of text before the code)I have "dep" at the end of the output string and I want it to display the number of dependants (which is z) the user inputs, as well as the percent associated with it so 0 dependants is 28% 2, is 20%, etc. at the very bottom is a function I used to calculate the withholding amount. Not exactly sure if I need to change anything else in the code, the way I wrote it is in steps so I assume if I change one thing the rest will change like a domino effect and I don't have to go back and change like 30 different things but let me know if you need clarification or if you notice something. C++: #include <iostream> using namespace std; int main(); double fed_withhold(double dep, double gross); double IRA_deduct(double gross); //dependants double dep = .28; double dep1 = .20; double dep2 = .18; double dep3 = .15; int y, z; double x, total, dpay; int main() { const int hours = 40, dhours = 50; const double prate = 1.5, drate = 2; bool isValid = false; int reg_hrs = 0, ot_hrs = 0, dt_hrs = 0; double withholding; while (!isValid)//ask for pay rate and validate { cout << "Enter your hourly rate of pay: "; cin >> x; isValid = x >= 0; if (!isValid) cout << "Error: the pay rate must be >= 0.\n"; }isValid = false; // reset the flag while (isValid == false)//ask for hours worked and validate { cout << "Enter the number of hours worked: "; cin >> y; isValid = y >= 0; if (!isValid) cout << "Error: the number of hours must be >= 0.\n"; }isValid = false; // reset the flag total = x * y;//pay rate * hours to give pay int overh = y - hours;// subtracts total hours by overtime (40 hours) to get overtime hours double overT = overh * prate * x; // multiples overtime hours by pay rate increase and by pay rate double bpmax = hours * x;//max base pay possible double gpay = bpmax + overT;//max base pay with overtime (gross pay) int althours = dhours - (hours + 1);//overtime hours double opmax = althours * prate * x;//overtime hours * over time pay rate increase * pay rate int doubh = y - dhours;// subtracts total hours by doubletime (50 hours) to get doubletime hours double dover = doubh * drate * x; // multiples overtime hours by double pay rate increase and by pay rate dpay = bpmax + dover + overT;// calculates the max base pay + double time pay + overtime pay double ira = IRA_deduct(gpay); double modifiedGross = gpay - ira; withholding = fed_withhold(dep, modifiedGross); double netPay = modifiedGross - withholding; cout.precision(2); if (y > hours && y <= dhours) { cout << "\nHours worked = " << fixed << y; cout << "\nHourly rate of pay =$" << x;
cout << "\nbase pay = $" << bpmax; cout << "\nOvertime pay=$" << overT;
cout << "\nGross pay = $" << gpay; } else if (y > dhours) {//double time "route" double alt_overh = althours * prate * x; //validate number of dependants do { cout << "Enter the number of dependents: "; cin >> z; isValid = z >= 0; if (!isValid) cout << "Error: the number of dependents must be >= 0.\n"; } while (isValid == false); double dpay_alt = gpay + dpay; cout << "\nHours worked = " << fixed << y; cout << "\nHourly rate of pay =$" << x;
cout << "\nbase pay = $" << bpmax; cout << "\nOvertime hours= " << althours; cout << "\nfederal tax brackets with " << z << " dependents is %" << dep; cout << "\nFederal tax withholding with " << z << " dependents=$" << dpay;
cout << "\ndoubletime hours= " << doubh;
cout << "\nGross pay = $" << gpay; cout << "\nnet pay=$" << dpay_alt;
}

else if (y <= hours) {

cout << "\nHours worked = " << fixed << y;
cout << "\nHourly rate of pay = $" << x; cout << "\nbase pay =$" << total;
cout << "\nGross pay = \$" << total;
}

}

double fed_withhold(double dep, double gross)
{
double taxRate, withholding;
switch (z)
{
case 0:
taxRate = dep;
break;
case 1:
taxRate = dep1;
break;
case 2:
taxRate = dep2;
break;
default:
taxRate = dep3;
}
withholding = dpay * taxRate;

return withholding;
}

double IRA_deduct(double gross)
{
double ira = 0;

if (gross > 400 && gross < 500)
{
ira = gross * 0.05;
}
else if (gross >= 500)
{
ira = gross * 0.10;
}

return ira;
}

#### WBahn

Joined Mar 31, 2012
26,398
I think I understand, so you're saying that "gross" on line 121 is the local variable and just a result of the calculations made in the computeIRA function? also, what does ira represent on lines 118 and 121?
IRA -- Individual Retirement Account

Similar threads