Say I have this:
struct Person {
char *name;
char *occupation;
int years_of_service;
float salary;
};
and this:
float calculate_salary(struct Person *who){
float basesalary;
char *doctor = "Doctor";
char *janitor = "Janitor";
int job1 = strcmp(who->occupation, doctor);
int job2 = strcmp(who->occupation, janitor);
if(job1 == 0){
basesalary = 10000.0;
}
else if(job2 == 0){
basesalary = 800.0;
}
return basesalary + basesalary*(who->years_of_service*0.1);
}
What is the right way to calculate a person’s salary?
in Python, I would do this in init:
self.salary = self.calculate_salary()
But since C is not OO, I assume that I must first create a person without a paycheck and set a paycheck after. Like this:
struct Person *joe = Person_create("Joe Alex", "Doctor",1);
joe->salary = calculate_salary(joe);
But I would like someone to better understand C to tell me if this is correct.
As a side note, is the line of compassion right? I find this very strange, should I use a switch instead?
source
share