Coders of helplife2.net... help me!

Status
Not open for further replies.

Rico

Tank
Joined
Aug 20, 2003
Messages
1,227
Reaction score
4
Hey folks,

I've been working on a programming project all week, and it's due on tuesday morning so I desperately need some help figuring out what I'm doing wrong here because I'm kind of stuck and can't seem to figure this one out.

I created a structure as such:
Code:
struct Employee
{
char *firstName, *lastName, *position;
int id;
double pay[2];
};

I need to create an array of Employee structure pointers, which are initialized to NULL. After that, I need to create a "Hire" function that will first ask the user for an employee ID #, then it will check the existing employees (if any) and if the ID is already present, notify the user and quit. If the ID isn't present, I need to allow the user to write into the structure's fields of firstName, lastName, and position.

Here's my main code:

Code:
#include "hw2DavidRicoConstants.c"

int main(void)
{




int employeeTotal = ZEROI;
int *employeeTotalPtr = &employeeTotal;
int choice, index;
struct Employee *employeePtr[ARRAY_ROWS] = { NULL };


//*employeePtr[ZEROI]->id = ZEROI;
for(index = ZEROI; index < ARRAY_ROWS; index++)
	{
	employeePtr[index] = malloc(1*sizeof(struct Employee));
	if(employeePtr[index] == NULL)
		{
		printf("Malloc failed, quitting.\n");
		exit(1);
		}
	}




do
{
	printf("Please choose an option: \n");
	printf("1: hire an employee\n");
	printf("2: pay an employee\n");
	printf("3: promote an employee\n");
	printf("4: transfer an employee\n");
	printf("5: print an employee\n");
	printf("6: print all employees\n");
	printf("7: exit program\n");
	scanf("%d", &choice);

	switch(choice)
	{
	case 1:
		hireEmployee(employeePtr, employeeTotalPtr);
		break;
	case 2: 
		//payEmployee(employeeId, employeePay);
		break;
	case 3: 
		//promoteEmployee(employeeId, employeePosition, employeePay);
		break;
	case 4: 
		//transferEmployee(employeeId, employeeFirst, employeeLast, employeePosition, employeePay, employeeTotalPtr);
		break;
	case 5:
		printEmployee(employeePtr, employeeTotalPtr);
		break;
	case 6: 
		//printAllEmployees(employeeId, employeePay, employeeFirst, employeeLast, employeePosition, employeeTotalPtr);
		break;
	case 7:
		exit(0);
		break;
	default:
		printf("Invalid choice. Please select a number between 1 and 7\n\n");
	}

}
while(choice < 8 && choice > 0);

return(ZEROI);
} 

////////////////////////////////////////////////////////////////////////////////////////////
//hireEmployee: Hires employees and changes the total number of employees currently on staff
///////////////////////////////////////////////////////////////////////////////////////////

void hireEmployee(struct Employee *employeePtr[],  int *employeeTotalPtr)
{

int tempId, index;

if(*employeeTotalPtr < ARRAY_ROWS)
	{
	printf("Current employee total: %d \n", *employeeTotalPtr);
	printf("Enter Employee id: ");
	scanf("%d", &tempId);
	
	
	for(index = ZEROI; index < ARRAY_ROWS; index++)
		{
		if(employeePtr[index]== NULL)
			{
			printf("Pointer is NULL. Replacing with 0 \n");
			*employeePtr[index]->id = ZEROI;
			}
		
			
		if(*employeePtr[index]->id == tempId)
			{
			printf("Employee with that ID already present \n");
			return;
			}
		
		}
		
		*employeePtr[*employeeTotalPtr]->id = tempId; 
                printf("\nEnter employee first name: ");
                scanf("%s", &employeePtr[*employeeTotalPtr]->firstName);
                printf("\nEnter employee Last name: ");
                scanf("%s", &employeePtr[*employeeTotalPtr]->lastName);
                printf("\nEnter employee position: ");
                scanf("%s", &employeePtr[*employeeTotalPtr]->position);
		if(employeePtr[*employeeTotalPtr] == NULL)
		{
			printf("Could not assign values. Exiting \n");
			exit(1);
			
		}
               	(*employeeTotalPtr) += INCREMENT;
               	//printf(" Aa %s \n", employeePtr[0]->firstName);
		hireEmployee(employeePtr, employeeTotalPtr);
                return;

	}
else
	{
	printf("Maximum employees reached, quitting\n");
	return;
	}



}

I really need some help here. I get as far as prompting the user, but whenever I try to pull anything out of the damned structure, I get a segmentation fault.

Any help? Pleeeeeease?
 
Nah it's not Source coding, so I figured I'd have better luck here.
 
i think what you're looking for is a linked list not an array of struct , or is that part of the requirements? , because with this method there will only be a finite number of employees. also im not sure about this because i havent really played with pointers but i think you need to pass the address when you're passing the array to hire employee.I'm going to play around with your code a bit. have you traced the exact point at which your error occurs by using breakpoints?
 
closed, go to the repost in coding forum
 
Status
Not open for further replies.
Back
Top