0

I want to make a friend function to display the Staff member that gets paid the most out of three staff members. I am unsure how to go about this. My code already works, just wanna add to it. What would the code be to do this and why does it work?? I was looking at examples of other's work but they don't really make sense to me. I hope you can help. Thank you!

#include <iostream>
using namespace std;

double Pay(double rate, double hours);

struct StaffInfo {
    int Staff_ID;
    double Hourly_Rate;
    double Weekly_Hours;
    int Dep_Num;
    double Paid;
    int Birth_Year;
};

int main()
{
    // # of Staff members
    StaffInfo staff[3];

    // For loop to get user input for the 3 staff members
    for (int i = 0; i < 3; i++) {

        cout << "Staff ID: ";
        cin >> staff[i].Staff_ID;
        cout << "Hourly Rate: ";
        cin >> staff[i].Hourly_Rate;
        cout << "Hours worked: ";
        cin >> staff[i].Weekly_Hours;
        cout << "Department Number: ";
        cin >> staff[i].Dep_Num;
        cout << "Birth Year: ";
        cin >> staff[i].Birth_Year;
        cout << endl;
        //Calls Pay function
        staff[i].Paid = Pay(staff[i].Hourly_Rate, staff[i].Weekly_Hours);
    }
    //Prints Staff# and how much they got paid
    for (int i = 0; i < 3; i++) {
        cout << endl << "Staff #:" << staff[i].Staff_ID << " pay is: $" << staff[i].Paid << endl;
    }
}

// Determine how much staff member gets paid
double Pay(double rate, double hours)
{
    double PayCheck = 0;

    //Determines if they worked overtime or not
    if (hours >= 40.00)
    {
        //Overtime pay rate
        PayCheck = ((rate * 1.5) * hours);
    }
    else if (hours < 40.00)
    {
        //No Overtime
        PayCheck = (rate * hours);
    }
    return PayCheck;
}
  • Why do you want it to be a `friend` function? Is it just to learn to write `friend` functions? Everything in `StaffInfo` is public; there's no need for `friend`. – Beta May 03 '20 at 20:59
  • @Beta It is just to learn how to use them. I thought I could do it with code I know works. – Just a viewer May 03 '20 at 21:01
  • @Justaviewer in your case a good example if to redefine `operator<<`and `operator>>`, and to have to put them `friend` for a reason to replace your `struct` by a `class` (or to add `private:` in your `struct` even this is not really common) – bruno May 03 '20 at 21:04
  • Do you mean you want it to be a member function? – anastaciu May 03 '20 at 21:06
  • @anastaciu `friend` means not anymore a member – bruno May 03 '20 at 21:07
  • I suggest you write a function `calcPay(const StaffInfo &staffer)` that calls `Pay` (bad name, BTW, it sounds like the verb) and returns the amount to be paid to that staffer. If you then make `Hourly_Rate` or `Weekly_Hours` private, then the function will not compile until you make it a `friend` of `StaffInfo`. – Beta May 03 '20 at 21:07
  • @bruno I'm just trying to find out the OP's intentions, he may be confused about these concepts. – anastaciu May 03 '20 at 21:08
  • @anastaciu yes that is possible – bruno May 03 '20 at 21:09

0 Answers0