* Returns the array index if the part number * * is found; otherwise, returns -1. * **********************************************************/ int find_part(int number, const struct part inv[], int np) {
int i;
for (i = 0; i < np; i++)
if (inv[i].number == number)
return i;
return -1;
}
/********************************************************** * insert: Prompts the user for information about a new * * part and then inserts the part into the inv * * array. Prints an error message and returns * * prematurely if the part already exists or the * * array is full. * **********************************************************/ void insert(struct part inv[], int *np)
{
int part_number;
if (*np == MAX_PARTS) {
printf("Database is full; can't add more parts.\n"); return;
}
printf("Enter part number: ");
scanf("%d", &part_number);
if (find_part(part_number, inv, *np) >= 0) {
printf("Part already exists.\n");
return;
}
inv[*np].number = part_number;
printf("Enter part name: ");
read_line(inv[*np].name, NAME_LEN);
printf("Enter quantity on hand: ");
scanf("%d", &inv[*np].on_hand);
(*np)++;
}