C Program Help - Calculating Federal Tax

This is a discussion on C Program Help - Calculating Federal Tax within the C Programming forums, part of the General Programming Boards category; Hello, I have almost all of this program working. Where I run into trouble is the final payroll report. The ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    Boston
    Posts
    7

    C Program Help - Calculating Federal Tax

    Hello,
    I have almost all of this program working. Where I run into trouble is the final payroll report. The federal tax, which is supposed to accumulate and add up is giving me a huge number that is totally wrong. Can someone please take a look and explain to me what I am doing wrong? Any help is appreciated! I have attached the code and the output.
    Attached Images Attached Images  
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Feb 2011
    Location
    Boston
    Posts
    7
    I have also tried setting sum_gross, sum_tax, sum_net to all equal zero but then I get an output of 0.00 for the Federal Tax.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    MetDude... next time post the code and attach the screen shot... ok?

    Your problem is that many of your variables are not correctly intialized and you are using them in logical comparisons and calculations from their uninitialized state.

    Code:
    float HoursWorked = 0;
    float FederalTax = 0; 
    // etc.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    First, some notes:
    • It's int main(void) - be explicit
    • Don't use gets, it's not safe
    • You have a getchar() after your return 0 in main. That will never be executed, move or delete it.


    Your problem is that you don't initialize sum_tax, or any sum variables, to zero (which you should). On top of that, you are multiplying, not adding:
    Code:
    sum_tax = sum_tax * federal_tax[x];
    You should consider a do-while loop for input:
    Code:
    result = do something
    while result invalid
        result = do something
    is equivalent to
    Code:
    do
        result = do something
    while result is invalid
    Lastly, I had some problems entering info. You use scanf for numbers, which doesn't get rid of the newline I typed in. You can find info about the problem and solution here: Cprogramming.com FAQ > Flush the input buffer

  5. #5
    Registered User
    Join Date
    Feb 2011
    Location
    Boston
    Posts
    7

    Thanks for the reply

    Hi Tater,
    Thanks for responding, I just tried your suggestions in my code and I am still getting a weird output for Federal Tax. Do you have any other suggestions? I'm trying to grasp this stuff but am having a hard time!!! Attached is the code and new ouput.

    Thanks!
    MetDude

    Code:
    #include <stdio.h>
    
    /* Define function for overtime pay */
    float calc_ot_pay (float hw, float hr)
    {
    	/* Declare variables */
    	float ot_pay;
    	
    	/* Perform calculations for overtime pay */
    	ot_pay = (hw - 40.0f) * hr * 1.5f;
    	
    	return ot_pay;
    } /* end calc_ot_pay */
    
    /* Define function for federal tax */
    float calc_fed_tax (float gp)
    {
    	/* Declare variables */
    	float fed_tax;
    	
    	/* Perform calculations for federal tax */
    	fed_tax = gp * .2f;
    	
    	return fed_tax;
    } /* end calc_fed_tax */
    
    /*Define function for net pay*/
    float calc_netpay (float gp, float fed_tax)
    {
    	/*Declare Variables*/
    	float netpay;
    	
    	/* Perform calculations for net pay */
    	netpay = gp - fed_tax;
    	
    	return netpay;
    } /* end netpay */
    
    /* Begin function main */
    
    int main(void)
    {
    	/* Variable Declarations */
    	float hours_worked = 0, hourly_rate = 0, gross_pay[10], overtime_pay = 0,
    	federal_tax[10], net_pay[10], sum_gross, sum_tax, sum_net;
    	int x = 0, num_emp;
    	char emp_name[10][30];
    	
    	/* Welcome and prompt for number of employees */
    	printf ("Welcome to the Payroll Calculator\n");
    	printf ("\nEnter the number of employees (1-10): ");
    	scanf ("%i", &num_emp);
    	printf ("\n");
    	
    	/* Check to make sure number of employees is between 1-10 */
    	while (num_emp < 1 || num_emp > 10)
    	{
    		printf ("*** Invalid number of employees. Please enter 1 - 10. ***\n");
    		printf ("Enter the number of employees (1-10): ");
    		scanf ("%i", &num_emp);
    		printf ("\n");
    	} /* end while */
    	
    	/* Prompt user for the employees name, hours worked, and hourly rate */
    	for (x = 0; x < num_emp; x++)
    	{
    		fflush(stdin);
    		printf ("Enter the name for employee #%i: ", x + 1);
    		gets (emp_name[x]);
    		printf ("Enter the number of hours employee #%i worked: ", x + 1);
    		scanf ("%f", &hours_worked);
    		
    		/* Check to make sure hours worked is greater than 0 */
    		while (hours_worked < 0)
    		{
    			printf ("*** Invalid number of hours worked. Please enter greater than 0. ***\n");
    			printf ("Enter the number of hours employee #%i worked: ", x + 1);
    	 		scanf ("%f", &hours_worked);
    		} /* end while */
    		
    		printf ("Enter the hourly rate of employee #%i: ", x + 1);
    		scanf ("%f", &hourly_rate);
    		printf ("\n");
    		
    		/* Check to make sure hourly rate is greater than 0 */
    		while (hourly_rate < 0)
    		{
    			printf ("*** Invalid hourly rate. Please enter greater than 0. ***\n");
    			printf ("Enter the hourly rate of employee #%i: ", x + 1);
    	 		scanf ("%f", &hourly_rate);
    	 		printf ("\n");
    		} /* end while */
    		
    		/* Determine the gross pay */
    		if (hours_worked > 40)
    		{
    			/* Call to function calc_ot_pay to determine overtime */
    			overtime_pay = calc_ot_pay (hours_worked, hourly_rate);
    			gross_pay[x] = 40 * hourly_rate + overtime_pay;
    		} /* end if */
    		else
    		{
    			gross_pay[x] = hours_worked * hourly_rate;
    		} /* end else */
    		
    		/* Determine federal tax */
    		/* Call to function calc_fed_tax to determine tax */
    		federal_tax[x] = calc_fed_tax (gross_pay[x]);
    		
    		/* Determine net pay */
    		/* Call to function calc_net to determine net pay */
    		net_pay[x] = calc_netpay (gross_pay[x], federal_tax[x]);
    		
    		/* Keep track of accumalted total of gross pay, net pay,
    		and federal tax */
    		sum_tax = sum_tax + federal_tax[x];
    		sum_net = sum_net + net_pay[x];
    		sum_gross = sum_gross + gross_pay[x];
    	} /* end for */
    	
    	/* Display the results for each of the employees entered */
    	printf ("              Payroll Report\n");
    	printf ("              --------------\n\n");
    	for (x = 0; x < num_emp; x++)
    	{
    		printf ("%s\n", emp_name[x]);
    		printf ("Gross Pay: $                %10.2f\n", gross_pay[x]);
    		printf ("Federal Tax: $              %10.2f\n", federal_tax[x]);
    		printf ("Net Pay: $                  %10.2f\n\n", net_pay[x]);
    	}
    	
    	/* Display the totals for the report */
    	printf ("              Report Totals\n");
    	printf ("              -------------\n\n");
    	printf ("Gross Pay: $                 %10.2f\n", sum_gross);
    	printf ("Federal Tax: $               %10.2f\n", sum_tax);
    	printf ("Net Pay: $                   %10.2f\n\n", sum_net);
    	
    	return 0;
    	getchar();
    
    } /* end main */
    Attached Images Attached Images  
    Last edited by MetDude21; 02-17-2011 at 01:00 PM. Reason: Code change

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are STILL not correctly initializing variables...

    For example...

    Code:
    	float hours_worked = 0, hourly_rate = 0, gross_pay[10], overtime_pay = 0,
    	federal_tax[10], net_pay[10], sum_gross, sum_tax, sum_net;
    
    // then...
    
    		sum_tax = sum_tax * federal_tax[x];
    		sum_net = sum_net + net_pay[x];
    		sum_gross = sum_gross + gross_pay[x];
    Think carefully... what values are in those three variables the first time you use them?
    (Hint: I don't know either.)

    And... why are you mutiplying for sum_tax... which is the one spitting out the huge number.
    Last edited by CommonTater; 02-17-2011 at 01:06 PM.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Location
    Boston
    Posts
    7
    I think that is part of my problem. I'm having a real problem understanding the arrays and intializing them. I've been over my book and countless articles to try and understand it and it just doesn't seem to click. Any suggestions

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MetDude21 View Post
    I think that is part of my problem. I'm having a real problem understanding the arrays and intializing them. I've been over my book and countless articles to try and understand it and it just doesn't seem to click. Any suggestions
    It's pretty simple really...
    When you create a variable inside a function (which is just about always) it's initial value is "undefined"... that is you can't know it's intial value so, with rare exception you have to intialize it to some safe value for your program.

    Without meaning to offend, the real problem here is that you are making assumptions that your compiler does not.

    And in this case it's not the arrays... it's three simple variables... as I showed you.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Location
    Boston
    Posts
    7
    Hi Tater,
    None taken, I agree with you. That is my biggest problem with this. What I'm thinking and what the compiler will do are totally different things. That's why I will be soo happy when this class is over. Your explanation is getting me on the right track. Can you please go into a little more detail about the arrays and intialization? I just want to be able to understand it once and for all and to be able to apply it to my program.

    Thanks for your help!!

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,481
    I don't think all this is guaranteed by the C standard, but it generally holds:

    Global variables and local static variables are initialized by the following rules:
    1. If an initializer value is specified, globals and local static variables are set to the initializer value at program startup
    2. Uninitialized globals and local static variables are set to zero at program startup.

    Local, automatic variables (regular function variables) and heap data are initialized by the following rules:
    1. If an initializer value is specified, automatic variables are set to that value upon function entrance.
    2. Uninitialized automatic variables, and all allocated heap data has an unspecified value, meaning it could be zero, or 1, or 17947835, or anything else.
    3. calloc allocates memory on the heap and zero-fills it (but that doesn't necessarily mean the value is actually zero for floats or NULL for pointers).

    Some of the reasoning behind this is:
    • Uninitialized globals (they're uninitialized, we don't care about their start value) can be bundled up into a block (called the BSS) that just says "reserve a bunch of bytes for data and set it all to zero" instead of actually having to specify a value for each of those bytes. It saves a lot of space in the executable. We can afford a little bit of time once at program startup.
    • Uninitialized stack variables aren't initialized because it's just too much work to re-initialize that data every time you make a function call. There's a stack portion of memory that tracks all the local function info, and it keeps changing and getting overwritten during normal execution, whenever you enter or leave a function, and sometimes during a function. This happens so often during the average program that it would waste too much time to set everything to zero every time.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MetDude21 View Post
    Hi Tater,
    None taken, I agree with you. That is my biggest problem with this. What I'm thinking and what the compiler will do are totally different things. That's why I will be soo happy when this class is over. Your explanation is getting me on the right track. Can you please go into a little more detail about the arrays and intialization? I just want to be able to understand it once and for all and to be able to apply it to my program.

    Thanks for your help!!
    Ummmm... error in perspective!

    Nobody ever gets the compiler to do things on their terms...

    C is a totally obedient idiot that only knows about a dozen words. To get along with it you have to speak it's language and you have to tell it absolutely everything, in the right order... If you tell it the wrong thing, it will blindly and obediently do the wrong thing. If you try to make it speak your language it will get confused and start screaming about syntax and stuff it can't do.

    You are instructing a totally obedient idiot with no morals... Govern yourself accordingly.

    For these variables...
    It's real real simple: If you declare it, you intialize it or you live with the consequences.

    Some variables may, in fact self-initialize... Those created outside any function, at page scope, most often do, but given who you're dealing with, that's not a good assumption.
    Last edited by CommonTater; 02-18-2011 at 11:54 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free program I'm sharing: ConvertEnumToStrings
    By Programmer_P in forum Projects and Job Recruitment
    Replies: 101
    Last Post: 07-18-2010, 12:55 AM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. End Program
    By jhwebster1 in forum C Programming
    Replies: 7
    Last Post: 02-24-2006, 08:30 AM
  4. Help with sample tax program
    By DaMonsta in forum C Programming
    Replies: 1
    Last Post: 05-15-2003, 03:45 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21