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!