Reza Namvar
Reza Namvar

Reputation: 129

struct nested in union nested in struct in C

I want to demonstrate an employee class who can have two types of payments; salaried or hourly. Based on user input, the employee's pay type is defined and I use one of the sructs in the union. I wrote this code for it:

struct Employee {
    int Id;
    char Dept;
    int Age;
    char PayClass;
    union PayType
    {
        struct Salaried {
            float MonthlyRate;
            int StartDate;
        } salaried;
        struct Hourly {
            float RatePerHour;
            int RegHours;
            int OvertimeHours;
        } hourly;
    } u;
};

The only way I can initialize fields in Salaried or Hourly is by writing the following:

int main()
{
    Employee emp;
    printf("Enter your ID?\n");
    cin >> emp.Id;
    printf("Enter your department:\nM for Math\nP for physics\nC for computer engineering\n");
    cin >> emp.Dept;
    printf("Enter your age\n");
    cin >> emp.Age;
    printf("Enter your pay type:\nS for salaried\nH for hourly");
    cin >> emp.PayClass;
    if (emp.PayClass == 'S') {
        printf("Enter your monthly rate\n");
        cin >> emp.u.salaried.MonthlyRate;
        //and other fields of Salaried struct
    }
    else {
        //initialize fields of Hourly struct
    }
}

Is this line cin >> emp.u.salaried.MonthlyRate; the best and most true way I can do this?

Upvotes: 0

Views: 128

Answers (1)

Mehroz Mustafa
Mehroz Mustafa

Reputation: 222

Yes your way of accessing the MonthlyRate is acceptable but if you want a cleaner way, you should use a mutator/setter. I have also inserted a condition which will check if the MonthlyRate is positive or not.

    #include <iostream>
    using namespace std;
    struct Employee {
        int Id;
        char Dept;  
        int Age;
        char PayClass;
        union PayType
        {
            struct Salaried {
            float MonthlyRate;
            int StartDate;
        } salaried;
            struct Hourly {
            float RatePerHour;
            int RegHours;
            int OvertimeHours;
        } hourly;
    } u;
    //- This Setter should do the trick
    void setMonthlyRate() {
        cin >> u.salaried.MonthlyRate;
        if (u.salaried.MonthlyRate < 0) {
            cerr << "Error! Invalid Input!" << endl;
            exit(1);
        }
    }
};
int main()
{
    Employee emp;
    printf("Enter your ID?\n");
    cin >> emp.Id;
    printf("Enter your department:\nM for Math\nP for physics\nC for computer engineering\n");
    cin >> emp.Dept;
    printf("Enter your age\n");
    cin >> emp.Age;
    printf("Enter your pay type:\nS for salaried\nH for hourly");
    cin >> emp.PayClass;
    if (emp.PayClass == 'S') {
        printf("Enter your monthly rate\n");
        //cin >> emp.u.salaried.MonthlyRate;
        emp.setMonthlyRate();
        //and other fields of Salaried struct
    }
    else {
        //initialize fields of Hourly struct
    }
}

Upvotes: 1

Related Questions