首页 > 解决方案 > 如何为此代码创建朋友功能?

问题描述

我想创建一个朋友功能来显示三名员工中获得最高薪水的员工。我不确定该怎么做。我的代码已经可以工作了,只是想添加它。执行此操作的代码是什么,为什么会起作用?我正在查看其他人的工作示例,但它们对我来说真的没有意义。我希望你能帮忙。谢谢!

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

标签: c++friend

解决方案


推荐阅读