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..

#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;
}

Recommended Answers

All 4 Replies

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?

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:

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) */
}

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...

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...


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....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.