• 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

    • I'm surprised they haven't found a way to fix that. As much as I don't like software wastes memory, a file manager is the kind of thing that makes sense to keep running in active memory for super-fast recall. I suspect that is why MS makes their File Explorer part of the main explorer.exe shell, so that it is guaranteed to always be running.
    • Seriously 1.8gig....so what. Any computer, if you are going to do serious work on, in 2026 should have a mimimum of 16gig and I would say 32-64gig is the way to go.
    • So again, you'd consider "I'm happy with my LG TV" to be a valid response to an article about PlayStation? I get the "I don't like any of their products" but how does that in any way invoke Linux? It just feels very random.
    • State of Decay 2, Blasphemous 2, and more join Xbox Free Play Days by Pulasthi Ariyasinghe The latest Free Play Days offer has just kicked off, giving Xbox players the chance to try out a new selection of games over the weekend. The offerings this time aren't as massive as last weekend, but there are still major releases for Xbox players to jump into. This includes Undead Labs' post-apocalyptic title State of Decay 2, as well as two Team17-published titles. Two of the games being offered this time are available to all Xbox players without needing any kind of Game Pass subscription. In the fully free-to-play section, you can jump into State of Decay 2. The title is both an action survival title and a community builder, letting players choose a map, set up their base, and try to keep the growing zombie threat at bay. Cooperative play is available too. Don't forget that the studio is preparing a third entry for 2027. Next, Blasphemous 2 drops in for Metroidvania and Soulslike fans, where The Penitent One returns for another adventure. The title has tough combat with multiple weapon options, deadly traps to avoid, and platforming sequences requiring a lot of patience and timing. Keep in mind though that the offer does have a 5-hour time limit attached to it. Lastly, Xbox Game Pass Ultimate, Premium, and Essential members can now try out the WWII-set first-person shooter Hell Let Loose. The multiplayer game offers 50 vs 50 combat in massive maps, with infantry, tank, and artillery options available for players. Here are the announced games and the platforms they are available to play on: Hell Let Loose (Xbox Series X|S, PC) State of Decay 2: Juggernaut Edition (Xbox Series X|S, PC) Blasphemous 2 (Xbox Series X|S, Xbox One) To easily find the titles on Xbox consoles, first head to the Store, and then in the sidebar, find the Home section. In there, open the Subscriptions tab. The Free Play Days collection will show up in this area. This week's Free Play Days promotions will end on Sunday, June 11, at 11:59 pm PT.
  • Recent Achievements

    • Week One Done
      FBSPL earned a badge
      Week One Done
    • One Year In
      Jim Dugan earned a badge
      One Year In
    • One Month Later
      Tommi118 earned a badge
      One Month Later
    • One Month Later
      sjbousquet earned a badge
      One Month Later
    • Week One Done
      sjbousquet earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      486
    2. 2
      PsYcHoKiLLa
      198
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      69
  • Tell a friend

    Love Neowin? Tell a friend!