d2jsp
d2jsp Forums > Programmer's Haven > C/C++/C# > Implement Pass-by-reference For 150fg > Details Inside
Add Reply New Topic New Poll
flowrevolution
#1 Oct 30 2017 03:23pm
Group: Member
Posts: 5,506
Joined: May 6 2008
Gold: 4,647.00
Implement A Pass-By-Reference statement in my code:



//payroll services.cpp
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;

int EmployeeType{ 0 };
double AnnualSalary{ 0 };
double HourlyRate{ 0 };
double HoursWorked{ 0 };
double CalcPay{ 0 };

double Pay(int HoursWorked, int HourlyRate){
return CalcPay;}
double Pay(double AnnualSalary) {
return CalcPay;}


int main()
{

do {

cout << "\n[Salary Employee: 1 , Hourly Employee: 2 , Exit Program: -1]\nEnter Employee Type:";
cin >> EmployeeType;
if (EmployeeType < 0.0)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\n\nINVALID SELECTION!" << endl;
cout << "Please restart using numeric values > 0, thank you!" << endl;

}

if (EmployeeType != -1)
{
switch (EmployeeType)

{
case 1:
{cout << "Enter the annual salary for employee: ";

cin >> AnnualSalary;
if (AnnualSalary < 0.0)

{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\n\nINVALID SELECTION!" << endl;
cout << "Please restart using numeric values > 0, thank you! \n";

}
else {
CalcPay = AnnualSalary / 52
cout << fixed << setprecision(2);
cout << "\n Payment: $" << CalcPay << endl;
}
if (!cin)
{
break;
}
else
{
break;
}
}
return 0;


case 2:
{cout << "Enter Employee Hourly Rate: ";

cin >> HourlyRate;
if (HourlyRate < 0.0)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\n\nINVALID SELECTION" << endl;
cout << "Please restart using numeric values > 0, thank you! \n";
}
else {
cout << "Enter Hours Worked for pay period: ";

cin >> HoursWorked;
if (HoursWorked < 0.0)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\n\nINVALID SELECTION" << endl;
cout << "Please restart using numeric values > 0, thank you!\n";
}
else {
CalcPay = HoursWorked * HourlyRate;
cout << fixed << setprecision(2);
cout << "\n Payment: $" << CalcPay << endl;
}
}
if (!cin)
{
break;
}
else
{
break;
}
}
return 0;
}

}
else
{
cout << "\nThank you for using Payroll services. \n";
}
} while (EmployeeType != -1);
return 0;
}
element11
#2 Oct 30 2017 03:35pm
Group: Member
Posts: 3,141
Joined: Jul 2 2009
Gold: 34.00
Im not sure what you need to do, are you trying to call Pay() with parameters passes by reference?

This post was edited by element11 on Oct 30 2017 03:40pm
flowrevolution
#3 Oct 30 2017 03:49pm
Group: Member
Posts: 5,506
Joined: May 6 2008
Gold: 4,647.00
Quote (element11 @ Oct 30 2017 09:35pm)
Im not sure what you need to do, are you trying to call Pay() with parameters passes by reference?


Thanks for responding. I pm'd you.

I need to use pass-by-reference anyway possible somewhere in this code. I tried to use pass by reference when calculating the annual Salary but I can't call the pass-by-reference function in a numeric expression so I don't think it is possible to use the weeks ( 52 weeks in a year ) within my annual salary calculation to do this.


I would also like to know If my function overloading is considered correct whether it does anything or not.


Also am I using a prototype? I believe I am but would like to make sure as well.
spaceleak
#4 Nov 2 2017 03:06am
Group: Member
Posts: 1,845
Joined: Sep 28 2014
Gold: 27,388.00
You need to do some more study and when posting for help it is a good idea to format code nicely.

Your Pay() function is currently passing arguments by value. To pass them by reference just do this:

Code
double Pay(int& HoursWorked, int& HourlyRate){
return CalcPay;}
double Pay(double& AnnualSalary) {
return CalcPay;}


Your function overloading is correct in the sense that you have multiple functions with the same name, in the same scope with different signatures.. Ignoring the fact you don't even call the function, you also don't even use the arguments in the function so it is pretty much pointless.

You aren't using prototypes, I think you would be referring to these lines:

Code
double Pay(int HoursWorked, int HourlyRate){
return CalcPay;}
double Pay(double AnnualSalary) {
return CalcPay;}


You're only defining the functions, there's no prototypes. Something like this would be probably what you need:

Code

// Prototypes
double Pay(int HoursWorked, int HourlyRate);
double Pay(double AnnualSalary);

// Definitions
double Pay(int HoursWorked, int HourlyRate){
return CalcPay;}
double Pay(double AnnualSalary) {
return CalcPay;}
Go Back To C/C++/C# Topic List
Add Reply New Topic New Poll