Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



C program, payroll issue


4 replies to this topic - - - - -

#1 showlow

    Neowinian

  • 9 posts
  • Joined: 25-April 12

Posted 29 April 2012 - 19:04

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



#2 bogas04

    This title is bogus

  • 2,686 posts
  • Joined: 15-May 09
  • Location: India
  • OS: Windows 8
  • Phone: Lumia 920

Posted 29 April 2012 - 19:18

View Postshowlow, on 29 April 2012 - 19:04, said:

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]);
}


#3 OP showlow

    Neowinian

  • 9 posts
  • Joined: 25-April 12

Posted 29 April 2012 - 20:01

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

#4 +Asik

  • 5,970 posts
  • Joined: 26-October 05

Posted 29 April 2012 - 20:07

View Postshowlow, on 29 April 2012 - 20:01, said:

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

#5 OP showlow

    Neowinian

  • 9 posts
  • Joined: 25-April 12

Posted 29 April 2012 - 20:33

No that was plenty. Appreciate the help