• 0

C program, payroll issue


Question

The payroll program is supposed to accept a structure of the id number, name, hours, and rate of each employee. Then, the program is supposed to calculte the gross pay or each employee and display it along with the id number and name. My program is running without any compiler errors but the displayed output is incorrect and it only display one set of numbers not the 6 for each employee.

#include <stdio.h>
#include <stdlib.h>
#define NUMEMP 6
struct NetPay
{
int id;
char name[10];
double rate, hours;
};
int main()
{
int i;
double payment[6];
struct NetPay employee[NUMEMP] = {{3462, "Jones", 4.62, 40.0},
		  {6793, "Robbins", 5.83, 38.5},
		  {6985, "Smith", 5.22, 45.5},
		  {7834, "Swain", 6.89, 40.0},
		  {8867, "Timmins", 6.43, 35.5},
		  {9002, "Williams", 4.75, 42.0}};
for (i =0; i < NUMEMP; i++)
{
  payment[i]= employee[i].rate*employee[i].hours;
}
  printf("The gross pay of %d %s is %lf\n", employee[i].id, employee[i].name, payment[i]);

system ("pause");
return 0;
}

Link to comment
https://www.neowin.net/forum/topic/1073793-c-program-payroll-issue/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

The payroll program is supposed to accept a structure of the id number, name, hours, and rate of each employee. Then, the program is supposed to calculte the gross pay or each employee and display it along with the id number and name. My program is running without any compiler errors but the displayed output is incorrect and it only display one set of numbers not the 6 for each employee.

#include <stdio.h>
#include <stdlib.h>
#define NUMEMP 6
struct NetPay
{
int id;
char name[10];
double rate, hours;
};
int main()
{
int i;
double payment[6];
struct NetPay employee[NUMEMP] = {{3462, "Jones", 4.62, 40.0},
		  {6793, "Robbins", 5.83, 38.5},
		  {6985, "Smith", 5.22, 45.5},
		  {7834, "Swain", 6.89, 40.0},
		  {8867, "Timmins", 6.43, 35.5},
		  {9002, "Williams", 4.75, 42.0}};
for (i =0; i < NUMEMP; i++)
{
  payment[i]= employee[i].rate*employee[i].hours;
}
  printf("The gross pay of %d %s is %lf\n", employee[i].id, employee[i].name, payment[i]);

system ("pause");
return 0;
}

You haz put the printf statement outside the loop , hence u see the result for last member :)

Fix :

for (i =0; i < NUMEMP; i++)
{
  payment[i]= employee[i].rate*employee[i].hours;
printf("The gross pay of %d %s is %lf\n", employee[i].id, employee[i].name, payment[i]);
}

  • 0

Ok thannk you! Hw would I display the total pay for all employees at the end?

Use a double that you declare before the loop and initialize at 0. Each time through the loop, add the employee's pay to it. Use a printf after the loop to display its value. I'd provide code but that would be doing your homework for you. :)
This topic is now closed to further replies.
  • Posts

  • Recent Achievements

    • Week One Done
      davidbazooked earned a badge
      Week One Done
    • One Month Later
      Jamswaz earned a badge
      One Month Later
    • Week One Done
      Jamswaz earned a badge
      Week One Done
    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
  • Popular Contributors

    1. 1
      +primortal
      514
    2. 2
      PsYcHoKiLLa
      185
    3. 3
      +Edouard
      159
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!