Thread: Have a few minor problems trying to finish this program

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    37

    Have a few minor problems trying to finish this program

    I'll list my problems first:
    1. How can I have the decimal points aligned in my output?
    2. One of the names being displayed in my output shows the first and second names of the array connected as one (John SmithJane Doe instead of just John Smith).
    3. When totalling the gross pay I am getting a random number and do not see where the mistake is in my code.

    They are all output problems. Here is my code as it appears right now:
    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 */
    
    /* Begin function main */
    
    main()
    {
    	/* Variable Declarations */
    	float hours_worked, hourly_rate, gross_pay[10], overtime_pay,
    	federal_tax[10], net_pay[10], sum_gross, sum_tax, sum_net;
    	int x = 0, num_emp;
    	char emp_name[30][10];
    	
    	/* 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 */
    		net_pay[x] = 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: $                %.2f\n", gross_pay[x]);
    		printf ("Federal Tax: $              %.2f\n", federal_tax[x]);
    		printf ("Net Pay: $                  %.2f\n\n", net_pay[x]);
    	}
    	
    	/* Display the totals for the report */
    	printf ("              Report Totals\n");
    	printf ("              -------------\n\n");
    	printf ("Gross Pay: $                 %.2f\n", sum_gross);
    	printf ("Federal Tax: $               %.2f\n", sum_tax);
    	printf ("Net Pay: $                   %.2f\n\n", sum_net);
    	return 0;
    
    } /* end main */
    and my trouble output:
    Code:
                  Payroll Report
                  --------------
    
    John SmithJane Doe <-- strange name eh?
    Gross Pay: $                400.00
    Federal Tax: $              80.00
    Net Pay: $                  320.00
    
    Jane Doe
    Gross Pay: $                830.00
    Federal Tax: $              166.00
    Net Pay: $                  664.00
    
    Dick Sally
    Gross Pay: $                547.20
    Federal Tax: $              109.44
    Net Pay: $                  437.76
    
                  Report Totals
                  -------------
    
    Gross Pay: $                 1270768584330927000000000000000000.00 <--why is this like this?!?!
    Federal Tax: $               355.44
    Net Pay: $                   1421.76
                                               ^
                                                |
                                         decimals need to be aligned
    I am in no way asking for the straight out answers, just need a pointer on where in my code I have made mistakes and I will correct them on my own. Any suggestions on what to do are most appreciated though!

    Thank you!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. It is not possible to store the name "John Smith" in your current setup -- as we hinted at in your other thread, a char[30][10] can hold 30 names of at most 9 characters each -- and John Smith is more than 9 characters long.

    2. You can use numbers between the % and f to set widths and precision for printing. Read up in any reasonable reference work on C under "printf format specifiers".

    3. Gross pay starts out at "random". You add something to it, and so its new value is "random". You add more things to it, and so its new value is "random". And then you add the last things to it, and so its final value is "random". If you don't want the value to be "random", you should probably start it off at some value other than "random".

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    Ok I got number 1 and 2 of my problems all set. I saw in my last thread you guys mentioning that, just forgot to switch the numbers around in the array. I also knew my answer to number 2, this being only the second assignment in my into to c 2 course, I am still a bit rusty from the last intro to c course, but it only took me a minute to remember that one.

    I am fixing my third problem right now.

    Thanks for the help!

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    IMO the array dimensions for storing emp data are switched around esp. since you are checking to make sure that no. of employees is between 1-10.
    Code:
    char emp_name[30][10];  /* implies 30 employees each of whose names is 9 characters os less */
    Last edited by itCbitC; 02-22-2009 at 06:53 PM.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    alright fixed everything.

    1. switched emp_name to [10][30]
    2. added a 10 to specify the width of the float numbers being displayed "%10.2f"
    3. set sum_gross, sum_net, and sum_tax to 0 so they would all start being accumalted from 0 and not some random number.

    thank you for the help guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM