943,682 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 664
  • C RSS
Jul 29th, 2009
0

Salary problem...(Beginner)

Expand Post »
//Make an interactive program that will ask for
hourly rate of the employee and the number of
hours worked in a day. As DOLE mandates, more
than 8 hours of work a day is considered OT
(overtime) so the wage will be 1.5 * hourly
rate after the 8 hours of work.using loops,
compute the weekly wage.Given 6 days of work
in a week.Output the gross salary and net
salary( gross salary less 12%.)//


I dont know how work with the overtime...

here's my code..
#include <stdio.h>

int main()
{
    int day,hRate,wHours,;
    int total = 0;
    int i = 1;
    float cTax,gross,salary,nSalary,aTotal;
    float tax = .12;
    
    printf("Enter Hourly Rate: ");
    scanf("%d",&hRate);
    printf("Enter number of days you've worked: ");
    scanf("%d",&day);
    
    if (i <= day)
    {
         while (i <= day)
         {
             printf("\nEnter number of hours you worked in  Day %d:  ",i);
             scanf("%d",&wHours);
             salary=wHours*hRate;
             printf("\nYour salary for  day %d is %.2f  \n",i,salary);
             i=i+1;
             total=total+salary;
         
         }
    }
    else
    if ( wHours > 8)
    {
         while (i <= day)
         {
             printf("\nEnter number of hours you worked in  Day %d:  ",i);
             scanf("%d",&wHours);
             salary=wHours*(hRate*1.5);
             printf("\nYour salary for  day %d is %.2f  \n",i,salary);
             i=i+1;
             total=total+salary; 
         }
    }
    aTotal = total;
    printf("\nYour weekly gross salary is %.2f", aTotal);  
    cTax =tax*total;                
    printf("\n\n the tax is %.2f",cTax );
    gross=aTotal-cTax;
    printf("\n\n weekly net salary is %.2f", gross);
    
    
    getchar();
    getchar();
}
Similar Threads
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
DoEds is offline Offline
63 posts
since Jun 2009
Jul 29th, 2009
0

Re: Salary problem...(Beginner)

You need to follow your code--execute your code on paper. Follow line by line and write down on paper what the values are for the variables. For example: if (i <= day). You set i=1. So unless the user enters a value for day of "0" this will always get executed. If a someone worked "0" days, then there is no reason to run this program. No work = no pay.

I didn't look through/correct all of your code, but I've modified your code a bit to help you along.

  1.  
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6. int day;
  7. int i = 1;
  8. //wHours should be float because people work partial hours sometimes
  9. float wHours;
  10. //hRate should be float because people aren't paid only in whole dollar amounts
  11. float hRate;
  12. float total = 0.0;
  13. float cTax,gross,salary,nSalary,aTotal;
  14. float tax = .12;
  15.  
  16. printf("Enter Hourly Rate: ");
  17. scanf("%f",&hRate);
  18. printf("Enter number of days you've worked: ");
  19. scanf("%d",&day);
  20.  
  21.  
  22. while (i <= day)
  23. {
  24. printf("\nEnter number of hours you worked in Day %d: ",i);
  25. scanf("%f",&wHours);
  26.  
  27. if (wHours > 8.0)
  28. {
  29. //what do you want to do if wHours > 8 ???
  30.  
  31. }
  32. else
  33. {
  34. salary=wHours*hRate;
  35. }
  36.  
  37. printf("\nYour salary for day %d is %.2f \n",i,salary);
  38. i=i+1;
  39. total=total+salary;
  40.  
  41. }
  42.  
  43.  
  44. aTotal = total;
  45. printf("\nYour weekly gross salary is %.2f", aTotal);
  46. cTax =tax*total;
  47. printf("\n\n the tax is %.2f",cTax );
  48. gross=aTotal-cTax;
  49. printf("\n\n weekly net salary is %.2f", gross);
  50.  
  51.  
  52. getchar();
  53. getchar();
  54. }
Last edited by cgeier; Jul 29th, 2009 at 12:27 pm.
Reputation Points: 41
Solved Threads: 15
Junior Poster
cgeier is offline Offline
104 posts
since Oct 2008
Jul 29th, 2009
0

Re: Salary problem...(Beginner)

Quote ...
I dont know how work with the overtime...

here's my code..
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. int day,hRate,wHours,;
  6. int total = 0;
  7. int i = 1;
  8. float cTax,gross,salary,nSalary,aTotal;
  9. float tax = .12;
  10.  
  11. printf("Enter Hourly Rate: ");
  12. scanf("%d",&hRate);
  13. printf("Enter number of days you've worked: ");
  14. scanf("%d",&day);
  15.  
  16. if (i <= day)
  17. {
  18. while (i <= day)
  19. {
  20. printf("\nEnter number of hours you worked in Day %d: ",i);
  21. scanf("%d",&wHours);
  22.  
  23. salary=wHours*hRate;
  24. printf("\nYour salary for day %d is %.2f \n",i,salary);
  25. i=i+1;
  26. total=total+salary;
  27.  
  28. }
  29. }
  30. else
  31. if ( wHours > 8)
  32. {
  33. while (i <= day)
  34. {
  35. printf("\nEnter number of hours you worked in Day %d: ",i);
  36. scanf("%d",&wHours);
  37. salary=wHours*(hRate*1.5);
  38. printf("\nYour salary for day %d is %.2f \n",i,salary);
  39. i=i+1;
  40. total=total+salary;
  41. }
  42. }
  43. aTotal = total;
  44. printf("\nYour weekly gross salary is %.2f", aTotal);
  45. cTax =tax*total;
  46. printf("\n\n the tax is %.2f",cTax );
  47. gross=aTotal-cTax;
  48. printf("\n\n weekly net salary is %.2f", gross);
  49.  
  50.  
  51. getchar();
  52. getchar();
  53. }
Instead of putting the whole thing in main(), why not make one or two different functions for computing the total salary and gross salary.
  1. const int OVERTIME_MULTIPLE = 1.5;
  2. const int HOURS_PER_DAY = 8;
  3.  
  4. int salaryPerDay(int numberOfHours, int rate) {
  5. if (numberOfHours <= HOURS_PER_DAY)
  6. return(numberOfHours * rate) ;
  7.  
  8. return ( (numberOfHours - HOURS_PER_DAY) *
  9. OVERTIME_MULTIPLE * rate +
  10. (HOURS_PER_DAY * rate) );
  11.  
  12. }

Now in main()
  1.  
  2. const int DAYS_IN_A_WEEK = 6;
  3. int weekNumber = 1;
  4. int weekSalary = 0;
  5. while (i <= day)
  6. {
  7. printf("\nEnter number of hours you worked in Day %d: ",i);
  8. scanf("%d",&wHours);
  9. /*Assuming the data entered is "what you expect"*/
  10. salary=salaryPerDay(wHours,hRate);
  11. /*Whether or not ,the person overworked,
  12.   the function will take care*/
  13. weekSalary += salary;
  14. printf("\nYour salary for day %d is %.2f \n",i,salary);
  15. i++;
  16. /*compute the weekly wage*/
  17. if (i % DAYS_IN_A_WEEK == 0) {
  18. printf ("Salary for #%d is %d",
  19. weekNumber, weekSalary);
  20. weekSalary = 0; /*A new week begins*/
  21. weekNumber++; /*Next week*/
  22. }
  23. total=total+salary;
  24.  
  25. }
Last edited by zalezog; Jul 29th, 2009 at 12:34 pm.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Aug 5th, 2009
0

Re: Salary problem...(Beginner)

Thanks for the idea guys i already solved it...
if you worked OT...The rate shud be (hRate*1.5)...Only the hours that you've OT will be multiplied the hRate by 1.5...Not the whole wHours...
So here's the final code...
#include <stdio.h>

int main()
{
    int day,hRate,wHours,;
    int total = 0;
    int i = 1;
    float cTax,gross,salary,nSalary,aTotal,OT;
    float tax = .12;
    
    printf("Enter Hourly Rate: ");
    scanf("%d",&hRate);
    printf("Enter number of days you've worked: ");
    scanf("%d",&day);
    
    if (i <= day)
    {
         while (i <= day)
         {
             printf("\nEnter number of hours you worked in  Day %d:  ",i);
             scanf("%d",&wHours);
             
             
             
             if ( wHours > 8)
             {
                OT=((wHours-8)*(hRate*1.5))+wHours*hRate;
                printf("\nYour salary for  day %d is %.2f  \n",i,OT);
                
             }
             else
             {
                  salary=wHours*(hRate);
                  printf("\nYour salary for  day %d is %.2f  \n",i,salary); 
                
                 }
             total=total+salary;    
             i++;
         }
    
    }
    aTotal = total;
    printf("\nYour weekly gross salary is %.2f", aTotal);  
    cTax =tax*total;                
    printf("\n\n the tax is %.2f",cTax );
    gross=aTotal-cTax;
    printf("\n\n weekly net salary is %.2f", gross);
    
    
    getchar();
    getchar();
}
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
DoEds is offline Offline
63 posts
since Jun 2009
Aug 5th, 2009
0

Re: Salary problem...(Beginner)

Click to Expand / Collapse  Quote originally posted by zalezog ...
Instead of putting the whole thing in main(), why not make one or two different functions for computing the total salary and gross salary.
  1. const int OVERTIME_MULTIPLE = 1.5;
  2. const int HOURS_PER_DAY = 8;
  3.  
  4. int salaryPerDay(int numberOfHours, int rate) {
  5. if (numberOfHours <= HOURS_PER_DAY)
  6. return(numberOfHours * rate) ;
  7.  
  8. return ( (numberOfHours - HOURS_PER_DAY) *
  9. OVERTIME_MULTIPLE * rate +
  10. (HOURS_PER_DAY * rate) );
  11.  
  12. }

Now in main()
  1.  
  2. const int DAYS_IN_A_WEEK = 6;
  3. int weekNumber = 1;
  4. int weekSalary = 0;
  5. while (i <= day)
  6. {
  7. printf("\nEnter number of hours you worked in Day %d: ",i);
  8. scanf("%d",&wHours);
  9. /*Assuming the data entered is "what you expect"*/
  10. salary=salaryPerDay(wHours,hRate);
  11. /*Whether or not ,the person overworked,
  12.   the function will take care*/
  13. weekSalary += salary;
  14. printf("\nYour salary for day %d is %.2f \n",i,salary);
  15. i++;
  16. /*compute the weekly wage*/
  17. if (i % DAYS_IN_A_WEEK == 0) {
  18. printf ("Salary for #%d is %d",
  19. weekNumber, weekSalary);
  20. weekSalary = 0; /*A new week begins*/
  21. weekNumber++; /*Next week*/
  22. }
  23. total=total+salary;
  24.  
  25. }
Can you show me the whole code? I really don't know how to use 2 functions.


Can you explain me what is this
weekSalary += salary; I don't know what is that...
Last edited by DoEds; Aug 5th, 2009 at 2:14 am. Reason: Mark as Solved.
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
DoEds is offline Offline
63 posts
since Jun 2009
Aug 5th, 2009
0

Re: Salary problem...(Beginner)

Aw...Sorry...

Here's the final code...

#include <stdio.h>

int main()
{
    int day,hRate,wHours,;
    int total = 0;
    int i = 1;
    float cTax,gross,salary,nSalary,aTotal;
    float tax = .12;
    
    printf("Enter Hourly Rate: ");
    scanf("%d",&hRate);
    printf("Enter number of days you've worked: ");
    scanf("%d",&day);
    
    if (i <= day)
    {
         while (i <= day)
         {
             printf("\nEnter number of hours you worked in  Day %d:  ",i);
             scanf("%d",&wHours);
             
             
             
             if ( wHours > 8 )
             {
                
               
                
                salary=((wHours - 8)*(hRate*1.5))+(8*hRate);
                
                
                printf("\nYour salary for  day %d is %.2f  \n",i,salary);
                
                
             }
             
             
             else
             {
                  salary=wHours*(hRate);
                  printf("\nYour salary for  day %d is %.2f  \n",i,salary); 
                  
                  
             }
             total=total+salary;    
             i++;
         }
    aTotal = total;
    printf("\nYour weekly gross salary is %.2f", aTotal);  
    cTax =tax*total;                
    printf("\n\n the tax is %.2f",cTax );
    gross=aTotal-cTax;
    printf("\n\n weekly net salary is %.2f", gross);
    
    
    }
    
    
    
    getchar();
    getchar();
}
Reputation Points: 6
Solved Threads: 0
Junior Poster in Training
DoEds is offline Offline
63 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Pointers and Sorting Array by Even and Odd numbers
Next Thread in C Forum Timeline: Pointer Issues in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC