On the payroll pointers program, I am struggling with a few things on the program.

The data from the terminal uses malloc, for an employee node. Dynamic Memory Allocation is used in the program.

For the name of each employee I can put in the First Name or First and Last Name together as one word, but if I put them as separate words it just gives me the first name and doesn't let the user enter the rest of the information requested by the program. When I select, a option for would you like to add another employee (y/n) I get a error dialog box debug error, Run-Time Check Failure #3- The variable 'num_emp' is being used without being initialized. What do they mean by used with being initialized? I don't know what they mean by unreferenced local variable. Any solutions that would fix the errors in the program?

#include "stdio.h"
#include "stdlib.h"

#define CHAR_SIZE 20
#define STD_HOURS 40.0
#define MAX_HOURS 158.0
#define OT_RATE   1.5

struct employee
{
  char  name[CHAR_SIZE];
  long  id_number;
  float wage;
  float hours;
  float overtime;
  float ot_pay;
  float gross_pay;
  float Gross;

  struct employee *next;
};


// Function gets hours for each employee


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 CalcOverPay (wage, ot_hrs)

float wage, ot_hrs;

    {
    float ot_rate,    /* ther rate the overtime is based on, such as */
		      /* time and a half                             */
          ot_pay;     /* The amount of pay that is from overtime     */

    /*  calculate the overtime pay  */
    ot_pay = ot_hrs * (wage * OT_RATE);

    /*  return the overtime pay to the calling function  */
    return (ot_pay);

    }  /*  end of function over_pay  */

float CalcGross(float hours, float wage, float overtime)
{
	float Gross;

	/* check for overtime and calculate gross*/
	if (overtime > 0) {
		Gross = (STD_HOURS * wage) /* regular pay */
                    + (overtime * OT_RATE * wage);     /* ot pay */
	} /* if */
	else  /* not ot */
	{
		Gross = wage * hours;
	} /* else */

	return (Gross);
} /* CalcGrossPay */



void print_list(emp1)
struct employee *emp1;
{
	struct employee *tmp; /* tmp pointer value to current node */
	int i = 0;            /* counts the nodes printed          */

	printf ("\n------------------------------------------------------------\n");
	printf ("  Name          Clock#       Wage     Hours    OT    Gross Pay\n");
	printf ("--------------------------------------------------------------\n");

	/* Start a beginning of list and print out each value               */
	/* loop until tmp points to null (remember null is 0 or false)      */
	for(tmp = emp1; tmp ; tmp = tmp->next)
	{
	    i++;



	    printf("%-15s, %6d,  $%6.2f   %.1f   %5.2f   $%6.2f\n",tmp->name, tmp->id_number,
						      tmp->wage, tmp->hours, tmp->ot_pay, tmp->gross_pay);
	}

	printf("\n\nTotal Number of Employees = %d\n", i);

}

/* end declarations */



int main()
{
	char   answer[80];
    int    more_data = 1;
	char   value;
	struct employee *emp_ptr;     /* always pointer to the first node */
    struct employee *temp_ptr;    /* points to the current node       */
    float ot_pay;                 /* overtime pay                     */
    int i,                        /* loop counter                     */
	num_emp;                  /* total number of employees        */

    struct employee *current_ptr, /* pointer to current node */
		    *head_ptr;    /* always points to first node */

    /* Set up storage for first node */
    head_ptr = (struct employee *) malloc (sizeof(struct employee));
    current_ptr = head_ptr;

    while (more_data)
	{

        printf("\nEnter employee name: ");
		scanf("%s", & current_ptr -> name);

		printf("\nEnter employee ID: ");
		scanf("%d", & current_ptr -> id_number);

        printf("\nEnter employee wage: ");
		scanf("%f", & current_ptr -> wage);

		printf("\nEnter hours worked for week: ");
		scanf("%f", & current_ptr -> hours);

	    printf("Would you like to add another employee? (y/n): ");
	    scanf("%s", answer);

	/*  loop for each employee  */
	for(i=0; i < num_emp; ++i)
	    {
	    /*  initialize the overtime pay  */
	    ot_pay = 0.0;

	    /*  test to see if the employee worked any overtime  */
	    if(temp_ptr->hours > STD_HOURS)
		{
		/*  call local function to calculate the overtime hours  */
		temp_ptr->overtime = CalcOT(temp_ptr->hours);

		/*  call local function to calculate the overtime pay  */
		ot_pay = CalcOverPay(temp_ptr->wage, temp_ptr->overtime);
		}
	    else 
		{
		temp_ptr->overtime = 0.0;
		}  /*  end of if statement  */

	    /*  call local funcion to calculate the gross pay  */
	    temp_ptr->Gross = CalcGross (temp_ptr->wage,temp_ptr->hours,ot_pay);
	    /*  move temporary pointer to next node in the linked list  */
	    temp_ptr = temp_ptr->next;

	    }  /*  end of for loop  */

	/* Ask user if they want to add another employee */
	if (value = toupper(answer[0]) != 'Y')
	   {
           current_ptr->next = (struct employee *) NULL;
	   more_data = 0;
	   }
        else
	   {
	   /* set the next pointer of the current node to point to the new node */
           current_ptr->next = (struct employee *) malloc (sizeof(struct employee));
	   /* move the current node pointer to the new node */
	   current_ptr = current_ptr->next;
	   }

	}

    /* print out listing of all employee id's and wages that were entered */
    print_list(head_ptr);

   printf("\n\nEnd of program\n");
   return 0;
 


	return 0;


} /* main */

1>------ Build started: Project: PayrollPointers, Configuration: Debug Win32 ------
1>Compiling...
1>payroll.c
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(46) : warning C4244: 'return' : conversion from 'double' to 'float', possible loss of data
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(61) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(56) : warning C4101: 'ot_rate' : unreferenced local variable
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(75) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(139) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(142) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(145) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(148) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(151) : warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files (x86)\microsoft visual studio 9.0\vc\include\stdio.h(306) : see declaration of 'scanf'
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(181) : warning C4013: 'toupper' undefined; assuming extern returning int
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(122) : warning C4101: 'emp_ptr' : unreferenced local variable
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(154) : warning C4700: uninitialized local variable 'num_emp' used
1>f:\c programming\payrollpointers\payrollpointers\payroll.c(160) : warning C4700: uninitialized local variable 'temp_ptr' used
1>Linking...
1>Embedding manifest...
1>Build log was saved at "file://f:\C Programming\PayrollPointers\PayrollPointers\Debug\BuildLog.htm"
1>PayrollPointers - 0 error(s), 13 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

>>scanf("%s", & current_ptr -> name);

scanf() will not allow you to enter a name with spaces. If you want the spaces, such as first <space> last name then use fgets() fgets(current_ptr->name, sizeof(current_ptr->name), stdin); One problem wth fgets() is that it will add the '\n' enter key to the end of the input stream, which you need to strip out

if( current_ptr->name[strlen(current_ptr->name)-1] == '\n')
   current_ptr->name[strlen(current_ptr->name)-1] = '\0';

>>scanf("%f", & current_ptr -> hours);
That will leave the '\n' Enter key in the keyboard buffer s that the next scanf() with %s or fgets() will just grab the '\n' and not allow you to enter any text. What you need to do is flush that '\n' out of the keyboard buffer. One way to do that is by just calling getcar(). Here is a little program to illustrate the problem. Run it a couple of times first as it is written and then again by commenting out the getchar() on line 9.

int main()
{
    char buf[255];
    float f;
    printf("Enter name\n");
    fgets(buf,sizeof(buf),stdin);
    printf("Enter pay\n");
    scanf("%f", &f);
    getchar();
    printf("Enter more text\n");

    fgets(buf,sizeof(buf),stdin);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.