I need to write a program that reads students' details from a file and stores them into an array of structures. After that, I need to filter the data by prompting user. My input file is as below:
Kristina
Science 30
Desmond
Geography 78
Fred
Science 87
Kristina
History 45
Desmond
Mathematics 34
I declare my struct to store the data as
typedef struct {
char name[102];
char subject[40];
int marks;
}Student;
I need to ask the user whether he wants to see the data by name or by subject. If he chooses name, then he will need to select whose data to be shown. For example, if he chooses the name Kristina
, the program should output
Kristina
Science 30
History 45
I managed to read from file and store the data into array of structs, but I am stuck in writing the function to output the filtered data. My code for the function is
Student* filter_name(char choice[], Student* res, int index)
{
int i;
choice[strcspn(choice,"\n")]=0;
for(i=0;i<index;i++)
{
if (strcmp(choice,res[i].name)==0)
{
return res;
}
}
}
After I run my code, it is still the same array of structs that was read from the file previously. I need the filtered array to sort it later in my program.
I need a copy of the elements that match, because i will need the existing array for another purpose as well.
Anyone can tell me how to return the filtered array?