943,598 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 646
  • C RSS
Oct 22nd, 2008
0

Simple program [urgent]

Expand Post »
I wrote this program for simple tax and gross pay calculation...
Im not able to execute the progam...Its compiling but the screen frozes while executing..
I know there is some simple mistake in my program... could any1 pls point out the mistake..


  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. float tax_cal(float gross_pay);
  6. float pay_cal(float payrate, int worked_hours);
  7.  
  8. void main()
  9. {
  10. //Variable declaration
  11. int payrate_index, worked_hours;
  12. float payrate,gross_pay, tax, net_pay;
  13.  
  14.  
  15. //Displaying the menu
  16. printf("\n");
  17. printf("***********************************************************\n");
  18. printf("* Enter the number corresponding to the desired pay rate: *\n");
  19. printf("* 1 - £8.50/hr 2 - £9.90/hr *\n");
  20. printf("* 3 - £22.40/hr 4 - £30.00/hr *\n");
  21. printf("* 0 - Exit the program *\n");
  22. printf("***********************************************************\n");
  23.  
  24. restart:
  25.  
  26. scanf("%d", &payrate_index);
  27. printf("\nPlease enter the number of hours per week - ");
  28. scanf("%d", &worked_hours);
  29.  
  30.  
  31.  
  32. switch(payrate_index)
  33. {
  34. case 0:
  35. exit(0);
  36.  
  37. case 1:
  38. payrate = 8.50;
  39. gross_pay = pay_cal(payrate, worked_hours);
  40. tax = tax_cal(gross_pay);
  41. net_pay = gross_pay - tax;
  42. printf("Net Pay - %.2f", net_pay);
  43. break;
  44.  
  45. /*case 2:
  46. payrate = 9.90;
  47. gross_pay = pay_cal(payrate, worked_hours);
  48. tax = taxcal(gross_pay);
  49. net_pay = gross_pay - tax;
  50. printf("Net Pay - %.2f", net_pay);
  51. break;
  52.  
  53. case 3:
  54. payrate = 22.40;
  55. gross_pay = pay_cal(payrate, worked_hours);
  56. tax = taxcal(gross_pay);
  57. net_pay = gross_pay - tax;
  58. printf("Net Pay - %.2f", net_pay);
  59. break;
  60.  
  61. case 4:
  62. payrate = 30.00;
  63. gross_pay = pay_cal(payrate, worked_hours);
  64. tax = taxcal(gross_pay);
  65. net_pay = gross_pay - tax;
  66. printf("Net Pay - %.2f", net_pay);
  67. break;
  68. */
  69. default :
  70. printf("Please select the number from the menu [1-4]\n");
  71. goto restart;
  72.  
  73. }
  74.  
  75. }
  76.  
  77. float pay_cal(float payrate, int worked_hours)
  78. {
  79. int normal_hours = 38;
  80. float gross_pay;
  81. float overtime_wage = 0.0;
  82.  
  83. if (worked_hours > normal_hours)
  84. overtime_wage = (float)(worked_hours - normal_hours ) * payrate * 1.5;
  85.  
  86. gross_pay = (float)(worked_hours * payrate) + overtime_wage;
  87. printf("\nGross Pay - %.2f", gross_pay);
  88. return gross_pay;
  89. }
  90.  
  91. float tax_cal(float gross_pay)
  92. {
  93. /*Tax rates: 15% of the first £300
  94. 20% of the next £200
  95. 25% of the next £150
  96. 30% of the rest
  97.   */
  98. float tax,tax1,tax2,tax3,tax4;
  99. if(gross_pay <= 300.0 && gross_pay > 8.5)
  100. {
  101. tax = 0.15*gross_pay;
  102. }
  103. else if(gross_pay>=301.0 && gross_pay <= 500.0)
  104. {
  105. tax1 = 0.15 * 300.0;
  106. tax2 = 0.2 * (gross_pay - 300.0);
  107. tax = tax1 + tax2;
  108. }
  109. else if(gross_pay>=501.0 && gross_pay <= 650.0)
  110. {
  111. tax1 = 0.15 * 300.0;
  112. tax2 = 0.2 * (500.0 - 300.0);
  113. tax3 = 0.25 * (650.0 - 500.0);
  114. tax = tax1 + tax2 + tax3;
  115. }
  116. else
  117. {
  118. tax1 = 0.15 * 300.0;
  119. tax2 = 0.2 * (500.0 - 300.0);
  120. tax3 = 0.25 * (650.0 - 500.0);
  121. tax4 = 0.3 * (gross_pay - 650.0);
  122. tax = tax1 + tax2 + tax3 + tax4;
  123. }
  124. printf("\nPayable Tax - %.2f", tax);
  125. return tax;
  126. }
Similar Threads
sbm
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbm is offline Offline
3 posts
since Oct 2008
Oct 22nd, 2008
0

Re: Simple program [urgent]

I've compiled and run it without problems;
Enter the number corresponding to ... : 1
Please enter the number of ... : 40
Output:
Gross Pay - 365.50
Payable Tax - 58.10
Net Pay - 307.40

Which was your test data for which the program crashed?
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
Chaster is offline Offline
68 posts
since Jun 2007
Oct 22nd, 2008
0

Re: Simple program [urgent]

There are lots of defects in this code but it works after minimal corrections.
1. Uncomment choices 2-4: replace bad function name from taxcal to tax_cal.
2. Avoid unnecessary label and goto statement. Use normal C loop in that case, for example:
  1. for (;;) {
  2. /* input payrate_index with prompting */
  3. if (!payrate_index)
  4. break;
  5. if (payrate_index < 0 || payrate_index > 4) {
  6. /* print bad choice message */
  7. continue;
  8. }
  9. /* input worked_hours with prompting */
  10. /* place switch statement here (default for code logics error only) */
  11. }
What for you ask user to input worked_hours for bad payrate_index?
In that case you can calculate more than one payrate w/o annoying program restart...
3. Append \n in format string literals.
4. Replace all float vars to double vars. I think worked_hours var must be double too.
5. Use int main(), not void main()!
6. Avoid a crudely constructed 1st message with cumbersome ********. Print a simple and clear welcome/help message.

Now you don't print a prompt message for bad payrate_index so it seems that the program is frozen...
Last edited by ArkM; Oct 22nd, 2008 at 11:27 am.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Oct 22nd, 2008
0

Re: Simple program [urgent]

Click to Expand / Collapse  Quote originally posted by Chaster ...
I've compiled and run it without problems;
Enter the number corresponding to ... : 1
Please enter the number of ... : 40
Output:
Gross Pay - 365.50
Payable Tax - 58.10
Net Pay - 307.40

Which was your test data for which the program crashed?
I used the same data..... but it worked after i delete my old obj file...
sbm
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbm is offline Offline
3 posts
since Oct 2008
Oct 22nd, 2008
0

Re: Simple program [urgent]

Click to Expand / Collapse  Quote originally posted by ArkM ...

What for you ask user to input worked_hours for bad payrate_index?
In that case you can calculate more than one payrate w/o annoying program restart...

Now you don't print a prompt message for bad payrate_index so it seems that the program is frozen...
thanks dude....
sbm
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sbm is offline Offline
3 posts
since Oct 2008

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: Quadratic Formula
Next Thread in C Forum Timeline: print 1 to 100 without using forloop





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


Follow us on Twitter


© 2011 DaniWeb® LLC