Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i can not figure out why a part of this code is not working, it seems to go into

ID: 3915139 • Letter: I

Question

i can not figure out why a part of this code is not working, it seems to go into the function but then ends the program without moving on to the next step of asking if the user would liek enter new employee data

here is my code as of now:

/*get gross and overtime pay for 1 or more employees*/

#include

#include /* for malloc */

#include /* for toupper */

/*Constants */

#define SIZE 5

#define STD_HOURS 40.0 /* standard weekly work hours*/

#define TIME_HALF 1.5 /*time and a half*/

struct employee

{

char first_name [10];

char last_name [10];

int id_number;   

float wage;

float hours;

float overtime;

float gross;

struct employee *next;

};

/* TODO - Add Function Prototypes as needed */

void print_list(struct employee *emp1);

void getOverTime (struct employee *emp1);

void getOverTime (struct employee *emp1)

{

printf("in getOverTime");

int i=0; /*loop index*/

struct employee *tmp; /* tmp pointer value to current node */

/* Process each employee one at a time */

for(tmp = emp1; tmp ; tmp = tmp->next)

{

i++;/*process each employee */

if (tmp->hours > STD_HOURS) /* ot */

{

printf(" in OT if");

tmp->overtime = tmp->hours - STD_HOURS;

}

else /* no ot */

{

printf(" in nO OT if");

tmp->overtime = 0;

}

} /*end loop */

printf(" TESTovertime: %8.2f",tmp->overtime);

}

void print_list(struct employee *emp1)

{

struct employee *tmp; /* tmp pointer value to current node */

int i = 0; /* counts the nodes printed */

/* 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++;

/* TODO - print other members as well */

printf(" Name: %s %s, ID:%6d, Wage: %8.2f ",tmp->first_name, tmp->last_name,tmp->id_number,

tmp->wage);

printf("Overttime: %8.2F", tmp->overtime);

}

printf(" Total Number of Employees = %d ", i);

}

int main ()

{

char answer[80]; /* to see if the user wants to add more employees */

int more_data = 1; /* flag to check if another employee is to be processed */

char value; /* gets the first character of answer */

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(" Please enter Employee Name:");

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

printf(" Enter how many hours worked:");

scanf("%f", & current_ptr -> hours);

printf(" Enter employee ID: ");

scanf("%i", & current_ptr -> id_number);

printf(" Enter employee hourly wage: ");

scanf("%f", & current_ptr -> wage);

getOverTime(head_ptr); /* ISSUE IS HERE I THINK*/

printf("Would you like to add another employee? (y/n): ");

scanf("%s", answer);

/* 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;

}

} /* while */

/* print out listing of all employee id's and wages that were entered */

print_list(head_ptr);

printf (" End of program ");

return (0);

}

Explanation / Answer

The error occurs in the getOverTime() . In this program we are passing the one Employee reference only ,So that temp->next generating the coredumped error.

We have to pass the current_ptr to the getOverTime() not head_ptr.

char answer; is enough ,no need to take char answer[80];

I have did small modifications ,so the program is executing.

Correct Code :

/*get gross and overtime pay for 1 or more employees*/
#include<stdio.h>
#include<stdlib.h> /* for malloc */
#include<ctype.h>/* for toupper */
/*Constants */
#define SIZE 5
#define STD_HOURS 40.0 /* standard weekly work hours*/
#define TIME_HALF 1.5 /*time and a half*/
struct employee{
   char first_name [10];
   char last_name [10];
   int id_number;
   float wage;
   float hours;
   float overtime;
   float gross;
   struct employee *next;
};

/* TODO - Add Function Prototypes as needed */
void print_list(struct employee *emp1);
void getOverTime (struct employee *emp1);
void getOverTime (struct employee *emp1){
   printf("in getOverTime");
   //int i=0; /*loop index*/
   struct employee *tmp; /* tmp pointer value to current node */
   /* Process each employee one at a time */
   //for(tmp = emp1; tmp ; tmp = tmp->next){
       //i++;/*process each employee */
   if(emp1!=NULL){
       tmp=emp1;
       if (tmp->hours > STD_HOURS) /* ot */{
           printf(" in OT if");
           tmp->overtime = tmp->hours - STD_HOURS;
       }
       else /* no ot */{
           printf(" in nO OT if");
           tmp->overtime = 0;
       }
   } /*end loop */
   printf(" TESTovertime: %8.2f",tmp->overtime);
}
void print_list(struct employee *emp1){
   struct employee *tmp; /* tmp pointer value to current node */
   int i = 0; /* counts the nodes printed */
   /* 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++;
       /* TODO - print other members as well */
       printf(" Name: %s %s, ID:%6d, Wage: %8.2f ",tmp->first_name, tmp->last_name,tmp->id_number,
       tmp->wage);
       printf("Overttime: %8.2F", tmp->overtime);
   }
   printf(" Total Number of Employees = %d ", i);
}
int main (){
   char answer; /* to see if the user wants to add more employees */
   int more_data = 1; /* flag to check if another employee is to be processed */
   char value; /* gets the first character of answer */
   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(" Please enter Employee Name:");
       scanf("%s %s", & current_ptr -> first_name ,& current_ptr -> last_name);
       printf(" Enter how many hours worked:");
       scanf("%f", & current_ptr -> hours);
       printf(" Enter employee ID: ");
       scanf("%i", & current_ptr -> id_number);
       printf(" Enter employee hourly wage: ");
       scanf("%f", & current_ptr -> wage);
       getOverTime(current_ptr); /* ISSUE IS HERE I THINK*/
       printf("Would you like to add another employee? (y/n): ");
       scanf("%s", &answer);
       /* Ask user if they want to add another employee */
       if ((value = toupper(answer)) != '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;
       }
  
   } /* while */
   /* print out listing of all employee id's and wages that were entered */
   print_list(head_ptr);
   printf (" End of program ");
   return (0);
}