So I'm fairly sure I have everything in the correct order, it compiles correctly, but when I try to run I just crash after the first Employee ID input. I've tried all i can think and have searched the web yet can't figure out why it crashes.

#include <stdio.h>
#include <string.h>

struct Employee
{
	int ID;
	char fName[51], lName[51];
	float pay;
};

int main()
{
	struct Employee eArray[5];

	int i,j,k,a,search,idSearch,paySearch;
	char lSearch[51];
	
	for(i=0;i<5;i++)
	{
		printf("Enter ID number for employee %d:", (i+1));
		scanf("%d", eArray[i].ID);
		for(j=(i+1);j<5;j++)
		{
			if(eArray[i].ID == eArray[j].ID)
			{
			printf("Duplicate ID number, enter again:");
			scanf("%d", eArray[i].ID);
			}
		}
		for(k=(i-1);k<i;k--)
		{
			if(eArray[i].ID == eArray[k].ID)
			{
			printf("Duplicate ID number, enter again:");
			scanf("%d", eArray[i].ID);
			}
		}

		printf("Enter first name for employee %d:", (i+1));
		gets(eArray[i].fName);
		printf("Enter last name for employee %d:", (i+1));
		gets(eArray[i].lName);
		printf("Enter hourly pay for employee %d:", (i+1));
		scanf("%f", eArray[i].pay);
	}

	printf("Search for employee by Last Name(1), ID(2), Hourly Pay(3):");
	scanf("%d", &search);

	if(search==1)
	{
		printf("Please enter Last Name to search for:");
		gets(lSearch);

		for(a=0;a<5;a++)
		{
			if(strcmp(eArray[a].lName, lSearch)==0)
			{
				printf("%s %s\nID:%d\n$/hr:%f\n", eArray[a].fName, eArray[a].lName, eArray[a].ID, eArray[a].pay);
			}
		}
	}
	else if(search==2)
	{
		printf("Please enter ID to search for:");
		scanf("%d", &idSearch);

		for(a=0;a<5;a++)
		{
			if(eArray[a].ID==idSearch)
			{
				printf("%s %s\nID:%d\n$/hr:%f\n", eArray[a].fName, eArray[a].lName, eArray[a].ID, eArray[a].pay);
			}
		}
	}
	else if(search==3)
	{
		printf("Please enter Hourly Pay to search for:");
		scanf("%d", &paySearch);

		for(a=0;a<5;a++)
		{
			if(eArray[a].pay==paySearch)
			{
				printf("%s %s\nID:%d\n$/hr:%f\n", eArray[a].fName, eArray[a].lName, eArray[a].ID, eArray[a].pay);
			}
		}
	}

	return 0;
}

lines 30-32: the value of k is a negative number when i is 0. Do the first time through using k as index is illegal because there is no such thing as -1 index of arrays.

Common mistake that everybody makes. :)
Line 21 : use "&" in scanf

Edited 4 Years Ago by subith86: n/a

Comments
Good observation -- I missed that :)

Common mistake that everybody makes. :)
Line 21 : use "&" in scanf

the same with line 27, 35 and 44 :)

Thanks for the replies everyone, I just took out the poorly done ID duplicate checker, and for some reason I was under the impression that I didn't have to put a & if it was part of a structure. But that seemed to fix the problem. But now I've encountered another one for the input of the first name. It will skip the gets(*first name variable*) and go straight to the input for the last name and continue on.

int main()
{
	Employee eArray[5];

	int i,a,search,idSearch,paySearch;
	char lSearch[51];
	
	for(i=0;i<5;i++)
	{
		printf("\nEnter ID number for employee %d:", (i+1));
		scanf("%d", &eArray[i].ID);
		printf("\nEnter first name for employee %d:", (i+1));
		gets(eArray[i].fName);
		printf("\nEnter last name for employee %d:", (i+1));
		gets(eArray[i].lName);
		printf("\nEnter hourly pay for employee %d:", (i+1));
		scanf("%f", &eArray[i].pay);
	}
This article has been dead for over six months. Start a new discussion instead.