• 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

    • The level of stupidity at Google and Microsoft is beyond belief. All they are doing by killing this is forcing people to jump over to Firefox and ditch them all together, therefore giving Firefox the market boost it needs to finally crush them all in the end.
    • HWMonitor 1.64 by Razvan Serea HWMonitor is a hardware monitoring program that reads PC systems main health sensors : voltages, temperatures, fans speed. The program handles the most common sensor chips, like ITE® IT87 series, most Winbond® ICs, and others. In addition, it can read modern CPUs on-die core thermal sensors, as well has hard drives temperature via S.M.A.R.T, and video card GPU temperature. Special hardware monitors such as abit® uGuru and Gigabyte® ODIN™ power supplies serie are supported too. HWMonitor 1.64 changelog: Intel Arc G3 & G3 Extreme (Panther Lake). Intel Core Ultra 5 250KF Plus (Arrow Lake Refresh). AMD Ryzen 7 7700X3D (Raphael). AMD Ryzen AI Max+ 495, 492, 488 (Gorgon Halo). AMD Ryzen AI Max 490, 485 (Gorgon Halo). AMD Ryzen AI Max PRO 495, 490, 485, 480 (Gorgon Halo). AMD Ryzen 9 9950X3D2 (Granite Ridge). AMD Ryzen 9 PRO 9965X3D, PRO 9945 (Granite Ridge). AMD Ryzen 7 PRO 9755, PRO 9745 (Granite Ridge). AMD Ryzen 5 PRO 9645 (Granite Ridge). AMD Ryzen AI 7/PRO 450G/GE (Gorgon Point 2). AMD Ryzen AI 5/PRO 440G/GE (Gorgon Point 2). AMD Ryzen AI 5/PRO 435G/GE (Gorgon Point 3). Support of HUDIMM and HSODIMM memory modules. New themes. New real-time graphs. Download: HWMonitor 1.64 | 3.4 MB (Freeware) Download: Portable HWMonitor 1.64 | 2.7 MB View: HWMonitor Homepage | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I have had a request since 2017 and so have many others that requested too and nothing has been done about it except its on our list to do.
    • I think it might be behind the trailer park amusements?
    • Some people don’t like them but plenty more enjoy what they have to offer. Check the value of the company.
  • Recent Achievements

    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      498
    2. 2
      PsYcHoKiLLa
      172
    3. 3
      +Edouard
      163
    4. 4
      Steven P.
      86
    5. 5
      ATLien_0
      77
  • Tell a friend

    Love Neowin? Tell a friend!