I have an array of structures called STUDENTs that has student names in it, and I need to be able to search this array for partial student names and get back full student records.
For example I need to be able to search for "Mercury, Fred" and get back a student record as well as be able to search for "M" and get back all names starting with "M."
This is the function I have for it:
Code:
int nSearch(STUDENT ary[], int size, char key[])
{
int i;
int results = 0;
for(i = 0; i < size; i++)
{
if(strncmp(key, ary[i].name, sizeof(key)) == 0)
{
printf("\n+---------------------+--------+--------+--------+--------+--------+---------+-------+\n");
printf("| Student Name | ID | Test 1 | Test 2 | Proj 1 | Proj 2 | Average | Grade |\n");
printf("+---------------------+--------+--------+--------+--------+--------+---------+-------+\n");
printf("| %-20s| %6d |%5d |%5d |%5d |%5d |%8.2f | %-2s |\n", ary[i].name, ary[i].id,
ary[i].s1, ary[i].s2, ary[i].s3, ary[i].s4, ary[i].avg, ary[i].grade);
results++;
}
}
if (results > 0) printf("+---------------------+--------+--------+--------+--------+--------+---------+-------+\n");
if (results == 0) return 0; //no match
}
Obviously not working. Anyone know what I can go?