Coders of hl2.net, need your help!

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?
 
Ach, you could have just waited for a mod to move it instead of duplicate thread D:
 
*employeePtr[index]->id = ZEROI;

Isn't it supposed to be :-

*employeePtr[index].id = ZEROI;

(since id is not a pointer)
 
it should be
(*employeePtr[index]).id = ZEROI;
also i just wanted to ask why you're making an array of pointers instead of making a pointer to an array of struct ?.
 
it's part of the assignment, we were told to create an array of pointers. I do agree an array of structs would be much easier.

edit: ugh, when I dereference like this: (*employeePtr[index]).id = ZEROI;
it gives me segmentation faults on runtime

This is my output:

Code:
Please choose an option: 
1: hire an employee
2: pay an employee
3: promote an employee
4: transfer an employee
5: print an employee
6: print all employees
7: exit program
1
Current employee total: 0 
Enter Employee id: 1

Enter employee first name: sadasd

Enter employee Last name: qweqwr

Enter employee position: xczxzxc
Current employee total: 1 
Enter Employee id: 2

Enter employee first name: waqweqwr

Enter employee Last name: asdasdasd

Enter employee position: xzczxczxc
Current employee total: 2 
Enter Employee id: 1
Employee with that ID already present 
Please choose an option: 
1: hire an employee
2: pay an employee
3: promote an employee
4: transfer an employee
5: print an employee
6: print all employees
7: exit program
5
Enter an employee ID: 2
No such employee
Please choose an option: 
1: hire an employee
2: pay an employee
3: promote an employee
4: transfer an employee
5: print an employee
6: print all employees
7: exit program
5
Enter an employee ID: 2
No such employee
Please choose an option: 
1: hire an employee
2: pay an employee
3: promote an employee
4: transfer an employee
5: print an employee
6: print all employees
7: exit program
5
Enter an employee ID: 1
Segmentation fault
 
I figured it out a few days ago. Seg faults were happening because while the memry for the pointer array was allocated, no memory had been allocated for the char * arrays I was trying to write to.

PWNED!
 
THREAD HIJACK

Hai u guyz

I recently installed Linux on my PC and figured I'd try out all the stuff that people say Linux is superior at (web development, programming, etc.) So I installed Emacs, Bluefish, GCC and GCJ and the standard libraries. After two days of frustration, I managed to compile my C++ programs by typing:
g++ <filename>

instead of:

gcc <filename>

Excellent. But now I want to try GCJ and whenever I compile this code (uploaded as a .txt file) I get this error:

Code:
/usr/lib/gcc/i586-mandriva-linux-gnu/4.1.1/../../../crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

Can any guru here help me?
 

Attachments

  • MyApplet.txt
    2.3 KB · Views: 189
PasteBin is handy for posting code, easier than attaching it.

I've never used GCJ, but google turns up this solution:
4.1 Why do I get undefined reference to `main' errors?

When using gcj to link a Java program, you must use the --main= option to indicate the class that has the desired main method. This is because every Java class can have a main method, thus you have to tell gcj which one to use.
 
Back
Top