Reputation: 13
Write a program that asks for an employee’s salary and years of service. Write a function which changes the salary depending on the years of service. If the employee has greater than 10 years of service. He or she will receive a 10% raise. Between 5 and 10 years, the raise is 5%. Everybody else will receive a 2% raise. Int calcbonus (int years_service); This function calculates and returns the employee’s bonus for the year. An employee will receive $500 for every 2 years of service. Void printcalculation (int years_service, float salary, int bonus); This function will display in a clear and professional manner all of the calculated information.
#include <stdio.h>
#include <conio.h>
float funsalary(float salary,float year);
float calcbonus(float year);
void printcalculation (float year,float salary,float bonus);
int main()
{
float year,salary,bon;
printf("Enter your salary");
scanf("%f",&salary);
printf("Enter your years of service");
scanf("%f",&year);
funsalary(salary,year);
bon = calcbonus(year);
printcalculation(year,salary,bon);
}
float funsalary(float salary,float year)
{
float raise;
if (year>10)
{
raise = salary*10/100;
}
else if (year>=5 && year<=10)
{
raise = salary*5/100;
}
else if (year<=2)
{
raise = salary*2/100;
}
return raise;
}
float calcbonus(float year)
{
float bonus;
bonus = (year/2)*500;
return bonus;
}
void printcalculation(float year,float raise,float bon)
{
float extra,total;
total = raise+bon;
printf("Total Income Is %f\n",total);
}
I input salary 1000, and year of service 2, the output should be greater than 1500 right? but it's displaying 1500.
Upvotes: 0
Views: 69
Reputation: 118
It is displaying 1500 because when you call funsalary(salary,year) function in the main you didn't store the return value of the function which yielded to your issue. You should change that line with:
salary = salary + funsalary(salary,year);
//alternatively salary += funsalary(salary,year);
in order to update the salary.
Alternatively you can assign a pointer to salary and then pass that reference to funsalary function(of course by updating parameters beforehand) as following:
int main{
int * salaryp;
salaryp = &salary;
funsalary(salaryp, year);
}
void funsalary(int * salaryp, year){
if (year>10)
{
*salaryp = *salaryp*10/100;
}
else if (year>=5 && year<=10)
{
*salaryp = *salaryp*5/100;
}
else if (year<=2)
{
*salaryp = salaryp*2/100;
}
}
Upvotes: 1