| | |
Simple program [urgent]
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 3
Reputation:
Solved Threads: 0
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..
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..
C Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<stdlib.h> float tax_cal(float gross_pay); float pay_cal(float payrate, int worked_hours); void main() { //Variable declaration int payrate_index, worked_hours; float payrate,gross_pay, tax, net_pay; //Displaying the menu printf("\n"); printf("***********************************************************\n"); printf("* Enter the number corresponding to the desired pay rate: *\n"); printf("* 1 - £8.50/hr 2 - £9.90/hr *\n"); printf("* 3 - £22.40/hr 4 - £30.00/hr *\n"); printf("* 0 - Exit the program *\n"); printf("***********************************************************\n"); restart: scanf("%d", &payrate_index); printf("\nPlease enter the number of hours per week - "); scanf("%d", &worked_hours); switch(payrate_index) { case 0: exit(0); case 1: payrate = 8.50; gross_pay = pay_cal(payrate, worked_hours); tax = tax_cal(gross_pay); net_pay = gross_pay - tax; printf("Net Pay - %.2f", net_pay); break; /*case 2: payrate = 9.90; gross_pay = pay_cal(payrate, worked_hours); tax = taxcal(gross_pay); net_pay = gross_pay - tax; printf("Net Pay - %.2f", net_pay); break; case 3: payrate = 22.40; gross_pay = pay_cal(payrate, worked_hours); tax = taxcal(gross_pay); net_pay = gross_pay - tax; printf("Net Pay - %.2f", net_pay); break; case 4: payrate = 30.00; gross_pay = pay_cal(payrate, worked_hours); tax = taxcal(gross_pay); net_pay = gross_pay - tax; printf("Net Pay - %.2f", net_pay); break; */ default : printf("Please select the number from the menu [1-4]\n"); goto restart; } } float pay_cal(float payrate, int worked_hours) { int normal_hours = 38; float gross_pay; float overtime_wage = 0.0; if (worked_hours > normal_hours) overtime_wage = (float)(worked_hours - normal_hours ) * payrate * 1.5; gross_pay = (float)(worked_hours * payrate) + overtime_wage; printf("\nGross Pay - %.2f", gross_pay); return gross_pay; } float tax_cal(float gross_pay) { /*Tax rates: 15% of the first £300 20% of the next £200 25% of the next £150 30% of the rest */ float tax,tax1,tax2,tax3,tax4; if(gross_pay <= 300.0 && gross_pay > 8.5) { tax = 0.15*gross_pay; } else if(gross_pay>=301.0 && gross_pay <= 500.0) { tax1 = 0.15 * 300.0; tax2 = 0.2 * (gross_pay - 300.0); tax = tax1 + tax2; } else if(gross_pay>=501.0 && gross_pay <= 650.0) { tax1 = 0.15 * 300.0; tax2 = 0.2 * (500.0 - 300.0); tax3 = 0.25 * (650.0 - 500.0); tax = tax1 + tax2 + tax3; } else { tax1 = 0.15 * 300.0; tax2 = 0.2 * (500.0 - 300.0); tax3 = 0.25 * (650.0 - 500.0); tax4 = 0.3 * (gross_pay - 650.0); tax = tax1 + tax2 + tax3 + tax4; } printf("\nPayable Tax - %.2f", tax); return tax; }
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:
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...
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:
c Syntax (Toggle Plain Text)
for (;;) { /* input payrate_index with prompting */ if (!payrate_index) break; if (payrate_index < 0 || payrate_index > 4) { /* print bad choice message */ continue; } /* input worked_hours with prompting */ /* place switch statement here (default for code logics error only) */ }
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.
•
•
Join Date: Oct 2008
Posts: 3
Reputation:
Solved Threads: 0
I used the same data..... but it worked after i delete my old obj file...
![]() |
Similar Threads
- urgent (C)
- help me,its urgent, PC Alarm clock ASM code (Assembly)
- help me fix my program(strtok,strcmp,array) (C)
- keep track print jobs before it gets sent off to the specified printer (Visual Basic 4 / 5 / 6)
- Dev C++ --> URGENT PLZ (C++)
- PLz its very urgent. (ASP)
- Urgent help needed Win_socket questions (C)
- JAVA HELP NEEDED, for plotting, URGENT (Java)
- Shell Scripting problem and related awk issues (Shell Scripting)
- solvetion of my code. (Java)
Other Threads in the C Forum
- Previous Thread: Quadratic Formula
- Next Thread: print 1 to 100 without using forloop
| Thread Tools | Search this Thread |
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm copyimagefile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide include incrementoperators input intmain() iso kernel keyboard kilometer km license linked linkedlist linux list lists locate looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf opensource openwebfoundation overwrite owf pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked research reversing scripting segmentationfault sequential single socket socketprogramming standard strchr string systemcall testing threads turboc unix urboc user variable wab whythiscodecausesegmentationfault windowsapi






