Pass-by-reference

I need to return taxes paid and net pay by pass referencing a gross pay overloaded function. Are the values returned from calling the overloaded function file stream objects? Can they be passed simply through a pass-by-reference function?


//Read Data from File, gather info and calculate pay, output data to file

while(counter < x)
{
inFile >> last_name >> first_name >> hours_worked >> hourly_pay;
outFile << first_name << " " << last_name << " ";
outFile << calculate_gross_pay(hours_worked,hourly_pay);
counter++;
outFile<<endl;
}
cout<<endl;
inFile.close();
outFile.close();
return 0;
}

// Gross pay calculation
double calculate_gross_pay(int hours_worked, double hourly_pay)
{
if (hours_worked <= 40 and hourly_pay == 0)
return hours_worked * 7.25;
if (hours_worked > 40 and hourly_pay ==0)
return 40*7.25+1.5*(hours_worked - 40)*7.25;
if(hours_worked <=40 and hourly_pay != 0)
return hours_worked*hourly_pay;
else
return 40*hourly_pay+1.5*(hours_worked - 40)*hourly_pay;
}
// Net pay, after taxes
void calulate_net_pay(int&calculate_gross_pay)
... lost lost lost!
Store the gross pay in a local variable and then pass that to calculate_net_pay:
1
2
3
4
double gross = calculate_gross_pay(hours_worked, hourly_pay;
double net = calcualte_net_pay(gross);
outFile << first_name << " " << last_name << " ";
outFile << gross << " " << net << endl;

This makes it easy to compute, easy to print, and easy to debug.
Thank you! I'm having an issue where the program will not recognize 'gross' when I try to pass it through void calculate_net_pay(gross). I'm not sure what I'm doing wrong.

void calculate_net_pay(int&);

....
double net = calcualte_net_pay(gross);

void calculate_net_pay(gross);
{ return gross * .25;
}

The error message reads 'Unknown type name 'gross' '
Error might be coming from here - if this is the function definition, you're missing the type to the left of gross in the parenthesis. And you wouldn't need the semicolon after the right parenthesis after gross.

1
2
3
void calculate_net_pay(gross);
{ return gross * .25;
}
Additionally, you are trying to return something from a function defined to not return anything.
AH! I typed in my code incorrectly.
I defined the function as:
double calculate_net_pay(int&)

called gross from stored local variable:
double gross = calculate_gross_pay(hours_worked,hourly_pay);

wrote function as:
double calculate_net_pay(gross)
{ return gross * .25;
}

I appreciate your help!
In the function definition you need the type.

1
2
3
double calculate_net_pay(??? gross)
{...
}
Thanks wildblue! This forum has been very welcoming!
Topic archived. No new replies allowed.