payroll program using structures

This is a discussion on payroll program using structures within the C Programming forums, part of the General Programming Boards category; I have a question, on the payroll program which now incorporates structures. I am taking the variables and putting them ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    19

    payroll program using structures

    I have a question, on the payroll program which now incorporates structures. I am taking the variables and putting them into structures. I am trying to fix this program it comes as command prompt and asks the hours for each employee after the five employees it doesn't put out the output.

    I get errors like "error C2440: 'function' : cannot convert from 'float' to 'float *'"

    I have a pic of the program and the source code.

    Code:
    #include <stdio.h>
    
    #define STD_HOURS 40.0
    #define OT_RATE 1.5
    #define SIZE 5
    
    float GetHours(int ClockNumber)
    {
    	float Hours;
    
    	printf("Enter hours worked for clock %06i: ", ClockNumber);
    	scanf("%f", &Hours);
    
    	return (Hours);
    }
    
    float CalcOT(float Hours)
    {
        /*  test to see if any overtime was worked  */
    	if (Hours > STD_HOURS)
    		return (Hours - STD_HOURS);
    	else  /*  no overtime was worked  */
    		return (0.0);
    }
    
    float CalcGross(float Hours, float Wage, float OT)
    {
    	float Gross;
    
    	/* check for overtime and calculate gross*/
    	if (OT > 0) {
    		Gross = (STD_HOURS * Wage) /* regular pay */
                        + (OT * OT_RATE * Wage);     /* ot pay */
    	} /* if */
    	else  /* not ot */
    	{
    		Gross = Wage * Hours;
    	} /* else */
    	
    	return (Gross);
    } /* CalcGrossPay */
    
    void PrintOutput(int ClockNumber[], float Wage[], float Hours[], float OT[], float Gross[])
    {
    	int i; /* loop index */
    
    	/*prints series for formatted dashes Horizontally*/
    	printf ("----------------------------------------------------------\n");
    	/*prints the employee's data header*/
    	printf ("\tClock#    Wage      Hours     OT     Gross\n"); 
    	/*Same as the first printf()*/
    	printf ("----------------------------------------------------------\n");
    
    	for (i = 0; i < SIZE; i++) {
    		printf ( "\t%06li    $%5.2f   %5.1f    %5.1f    $%6.2f\n",ClockNumber[i],Wage[i],Hours[i],OT[i],Gross[i]); /*prints the employee's data according to the given precision*/
        } /* for */
    
    }
    
    
    
    
    int main()
    {
    
    	int i;
    
    struct employee
    {
      int  ClockNumber;
      float Wage;
      float Hours;
      float OT;
      float Gross;
    };
    
    	struct employee emps[SIZE] =
    	{
    
    	  {98401, 10.60},
    	  {526488, 9.75},
    	  {765349, 10.5},
    	  {634645, 12.25},
    	  {127615, 8.35}
            };
                                                              /* loop index */
    
    	printf ("\tThis is a program to calculate gross pay.\n\n"); /*print the string of characters on the screen*/
    
    	/*prompt for the input values from the screen*/
    	for (i = 0; i < SIZE; i++) {
    		emps[i].Hours = GetHours(emps[i].ClockNumber); 
    		emps[i].OT = CalcOT(emps[i].Hours);
    		emps[i].Gross = CalcGross(emps[i].Hours, emps[i].Wage, emps[i].OT);
    	}
    
    	// the printing would make a nice function as well
    	PrintOutput(emps[i].ClockNumber,emps[i].Wage,emps[i].Hours,emps[i].OT,emps[i].Gross);
    
    	return 0;
    
    } /* main */
    Attached Images Attached Images  

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    pic is not necessary.
    Just post(copy & paste) compiler error/warning messages(which function,line number important!) and relevant code.

    You have array of struct.

    Code:
    // expect array of int,float...
    void PrintOutput(int ClockNumber[], float Wage[], float Hours[], float OT[], float Gross[])
    
    // what you pass here?
    PrintOutput(emps[i].ClockNumber,emps[i].Wage,emps[i].Hours,emps[i].OT,emps[i].Gross);
    I don't know why you don't make struct definition global rather than local to main????
    Last edited by Bayint Naung; 07-24-2010 at 11:09 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two problems pop up right away:

    1) Your program calls functions with two parameters, but the function is declared to have just one parameter.

    That won't work - the call and the declaration of a function, should have the same number of parameters.

    2) If you want to change something in a variable (even an array), in a function you call, you have to either make the variable global (a bad idea usually), or you have to pass the address of the variable, to the function that's going to be making some change to it.

    Have you noticed that with scanf(), you have to include the & "address of" operator, so it can change the variable, but with printf(), you don't need the address of a variable to print it?
    Same with your functions. If you DON'T want to change the value of a parameter, then you don't need the address of that variable - so a regular copy of it, is just fine.

    If you want to change the value of a variable in a different function, you need to pass the address of the variable you want to change. (Unless it's global variable, but that's a bad idea, generally).

    And that's the errors I see that your compiler is reporting.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    Payroll program using Arrays and functions
    Keep the same question on the same thread.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi, Quiz C program Assignment updated
    By Eman in forum C Programming
    Replies: 19
    Last Post: 11-22-2009, 03:50 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. Help with a payroll program
    By xc-racer in forum C++ Programming
    Replies: 10
    Last Post: 02-22-2004, 10:31 PM
  4. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21